To store information that is not appropriate to store client-side, we use sessions. Lasso has built in session handling, and deals with the setting and retrieval of the cookie itself. It will automatically set and retrieve the session id, which is the only thing stored client-side.
To set up a new session, we first start the session, then add to it the variables we would like to store in it. Those variables are stored within Lasso's session database.
// Start the session.
session_start(
'mySessionName',
-expires = 1440,
-usecookie=true
)
// Add variables to the session
if(session_result('mySessionName') != 'load') => {
session_addVar('mySessionName', 'sv_userId')
session_addVar('mySessionName', 'sv_userName')
session_addVar('mySessionName', 'sv_userEmail')
session_addVar('mySessionName', 'sv_favouriteColour')
}
!var_defined('sv_userId') ? var('sv_userId' = integer)
!var_defined('sv_userName') ? var('sv_userName' = string)
!var_defined('sv_userEmail') ? var('sv_userEmail' = string)
!var_defined('sv_favouriteColour') ? var('sv_favouriteColour' = 'red')
Lines 2-6: Initializing the session. This needs to happen on every page you wish the session information to be readable and writable. Here we start a session named 'mySessionName' with an idle expiry of 1440 minutes (1 day) and set to track via cookie.
Lines 9-14: The "if" conditional here checks to see if the session_result shows a new session and adds new vaiables to the session if it is new.
Lines 16-19: Each line in this section is checking if a variable has been defined, and if not then the variable is defined and declared with a default type and value.
The variables added to the session can now be used just like any other variable in the page. These variables are both readable and writiable. This means that when the value is changed, it will be stored in the session with the new value.
On subsequent pages if the session is initialized, you will be able to continue reading and modifying these variables.