Session Class
The session class allows you to maintain state for your application in the stateless environment of the web.
It allows you to store variables on the server using a variety of storage solutions, and recall these variables on the next page request.
The static methods documented on this page use the session driver configured in the configuration via the driver setting.
When you have set the auto_initialize setting to true, a session will be initialized when the Session class is loaded.
If it is set to false, you will have to use one of the methods below, or start a session instance manually, to initialize the session.
If your application needs session support, either "always_load" your session class, or make sure your (base) controllers
will load it. If you do load it, but you choose not to auto_initialize the session, and you don't use any of the session methods,
the session will not be refreshed! This can cause unexpected behaviour of your application due to an expired session.
There session drivers have a built-in garbage collection mechanism to remove stale entries. Storage backends that
have built-in support for data expiration, such as APC, Memcached or Redis, will use that feature, and
will auto expire stale cache entries.
Storage backends that don't, for example the database or file system drivers, use the gc_probability setting to determine if
garbage collection is needed. If so, they will run it in a shutdown event, after the page has been send to the user.
Downside of this method is that if this takes some time, the page will not finished to be "loading..." until GC is completed.
When using sessions, make ABSOLUTELY sure that the timezone configured in your app/config/config.php and/or your
php.ini file matches the timezone set on your server. Since cookie expiry timestamps are in GMT, expiry time calculation goes
horribly wrong when you have a timezone mismatch, ranging from not expiring properly to session cookies not set at all because they arrive
at the browser already expired.
The instance method returns the default session instance, or a specific instance, identified by name.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$instance |
null
|
Cookiename (as defined in config/session.php) that identifies the required session instance. |
|
Returns |
mixed - session object, or false in case the requested instance does not exist. |
Example |
$session = Session::instance();
$session = Session::instance('myappcookie');
|
The set method allows you to set a session variable.
Static |
Yes |
Parameters |
Param |
Type |
Default |
Description |
$variable |
string|array |
required |
Name of the session variable to set or associative array of values. |
$value |
mixed |
null
|
The session variables value.
This can be any data type, but pay attention when storing objects in the session, as session data is serialized, and there are restrictions to serializing an object.
|
|
Returns |
FuelCoreSession_Driver - is chainable |
Example |
Session::set('userid', $userid);
Session::set('array', array('varA', 'varB', 'varC' => array('val1', 'val2'));
Session::set(array(
'userid' => $userid,
'has_cookies' => function()
{
return (bool) \Cookie::get('has_them', false);
}
));
Session::set('userid', $userid)->set('foo', 'bar');
|
The get method allows you to retrieve stored variables from the session.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$variable |
null
|
Name of the session variable to get. If not specified, all session variables are returned. |
$default |
null
|
Default value to return in case the requested variable does not exist. If no default is given, the method will return null. |
|
Returns |
mixed - Dependent on the type of the stored $variable. The method returns null if the requested variable does not exist. |
Example |
$userid = Session::get('userid');
if ( $userid === false )
{
echo "no user is logged in";
}
$arr = Session::get('array');
$arr = Session::get('array.varC');
$vars = Session::get();
|
The delete method allows you to delete a stored session variable.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$variable |
required |
Name of the session variable to delete. |
|
Returns |
FuelCoreSession_Driver - is chainable |
Example |
Session::delete('userid');
Session::delete('array.varC');
|
The set_flash method allows you to set a session flash variable. Flash variables have a limited lifespan. Depending on the configuration, they will be deleted after the next page request, or after they are retrieved.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$variable |
required |
Name of the session flash variable to set. |
$value |
null
|
The session flash variables value.
This can be any data type, but pay attention when storing objects in the session, as session data is serialized, and there are restrictions to serializing an object.
|
|
Returns |
FuelCoreSession_Driver - is chainable |
Example |
Session::set_flash('step', 2);
Session::set_flash('array', array('varA', 'varB', 'varC' => array('val1', 'val2')));
|
This method supports limited 'dot-notation' to save an element in a multidimensional array. Only the top-level is versioned, the
entire multidimensional array will expire at the same time.
The get_flash method allows you to get a session flash variable. Flash variables have a limited lifespan. Depending on the configuration, they will be deleted after the next page request, or after they are retrieved.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$variable |
required |
Name of the session variable to get. |
$default |
null
|
Default value to return in case the requested variable does not exist. If no default is given, the method will return null. |
$expire |
false
|
if true, the session variable expires immediately, even if it is set in the same request. |
|
Returns |
mixed - Dependent on the type of the stored $variable. The method returns null if the requested variable does not exist. |
Example |
$step = Session::get_flash('step');
|
This method supports 'dot-notation' to retrieve an element from a multidimensional array.
The keep_flash method resets a flash variable stored in the session to an 'unrequested' state. This allows you to get a flash variable, and still pass it on to the next page request.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$variable |
required |
Name of the flash variable to keep. |
|
Returns |
FuelCoreSession_Driver - is chainable |
Example |
Session::keep_flash('step');
|
The delete_flash method allows you to delete a stored flash variable.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$variable |
required |
Name of the flash variable to delete. |
|
Returns |
FuelCoreSession_Driver - is chainable |
Example |
Session::delete_flash('userid');
|
The create method allows you to create a new session. If a session is already present, it will be destroyed when the new one is created.
Static |
Yes |
Parameters |
None |
Returns |
FuelCoreSession_Driver - is chainable |
Example |
Session::create();
|
The destroy method allows you to destroy an existing session.
Static |
Yes |
Parameters |
None |
Returns |
FuelCoreSession_Driver - is chainable |
Example |
Session::destroy();
|
The read method allows you to manually read a session. The session is automatically read when the session class is initialized, so under normal circumstances there is no need to use this method.
Static |
Yes |
Parameters |
None |
Returns |
FuelCoreSession_Driver - is chainable |
Example |
Session::read();
|
The write method allows you to manually write the session. Under normal circumstances, the session is automatically written when the script ends.
Static |
Yes |
Parameters |
None |
Returns |
FuelCoreSession_Driver - is chainable |
Example |
Session::write();
|
The rotate method allows you to manually rotate the session id.
Under normal circumstances, the session id is automatically rotated periodically,
as defined in the configuration. You might want to manually rotate it as an extra
security measure, for example when you change the rights of the logged-in user.
Static |
Yes |
Parameters |
None |
Returns |
FuelCoreSession_Driver - is chainable |
Example |
Session::rotate();
|
The key method allows you retrieve elements of the session key,
which uniquely identifies the session.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$name |
optional |
Name of the key element. By default, it is set to 'session_id'.
Other elements that may be available are 'ip_hash', 'created', 'updated'
'user_agent' and 'payload'.
|
|
Returns |
mixed - element value, or false if the requested element does not exist. |
Example |
$session_id = Session::key('session_id');
|