The Session service provides an a simple interface for using native $_SESSION data.

The session is not started until the session service is accessed, so in its __construct() method session_start() is called.

Note worthy is that when the Disco application is setup these php.ini settings are changed:

ini_set('session.use_trans_sid', 0);
ini_set('session.use_only_cookies', 1);

Basic Examples #

// Set a session variable
$adminId = 15;
Session::set('admin', $adminId);

// Get a session variable
$adminId = Session::get('admin');

// Check if a session exists
   // We have an admin session

// Check if the session has any number of sessions
$session = ['admin','super','test'];

   // We have one of the sessions

// Use the flash methods to transfer data between requests,
// meaning it is only kept from one request to the next, from the time it is set
Session::setFlash('form-error-username','You did not set a username!');

    $error = Session::getFlash('form-error-username');

// You can set complex data types like objects and arrays using the complex methods,
// this performs serialization of the data
Session::setComplex('user', Array('id' => 234, 'name' => 'Jimmy John'));

    // Get the complex data, un-serializing it before returning it
    $user = Session::getComplex('user');

// You can also flash complex data
Session::setComplexFlash('form-errors', [ 'email' => 'Invalid email', 'password' => 'Invalid password (must be at least 4 characters)']);

    $errors = Session::getComplexFlash('form-errors');

Regenerating the Session ID #

Calls the native php function session_regenerate_id() generating a new unique session id.


It is recommended that you call Session::regen() after each login of a user.

Reset the Session #

Calls the native php function session_regenerate_id(true) generating a new unique session id and deleting the old session file.


Flush/Empty/Unset the Session #

Calls the native php function session_unset() clearing all data from the session.


Destory #

Calls the native php function session_destory() deleting the current session file.