Sessions


1Presentation

The session system allows temporary storage of variables, which are linked to a visitor. The advantage is to perform processing in a controller as a function of the processing carried out previously in the controllers executed beforehand.
Each session is linked to a browser by means of a cookie placed on the first access. This is transparent because it is managed by the framework.

The session management object is created automatically by Temma, if the configuration provides for it.


2Configuration

In the etc/temma.php file, you can define how sessions are stored (see configuration documentation).

Please note that if no source is specified, Temma uses PHP's native session mechanism. Note that PHP stores session data in files, which are locked when accessed. This means that only one request can access them at a time, which can create lockouts if you have long-running requests (for example, if you use event controllers).


3Session object

In controllers, the session object is available by writing:

$this->_session

In the other objects managed by the dependency injection component, the session object can be accessed by writing:

$this->_loader->session

4Reading data

To read data from the session, just use the object as an associative array:

$currentUser = $this->_session['user'];

To find out if a session variable exists, or if it is empty:

// is the variable defined?
if (isset($this->_session['myVariable']))
    doSomething();

// is the variable empty?
if (empty($this->_session['myOtherVariable']))
    doSomethingElse();

// undefined variable: use a default value
$var = $this->_session['myVariable'] ?? 'defaultValue';

// undefined or empty variable: use a default value
$var = $this->_session['myVariable'] ?: 'defaultValue';

With the get() method, you can read a session variable, specifying a default value which is returned if the data is not present in the session:

$var = $this->_session->get('myVariable', 'Default value');

It is possible to retrieve all the session variables with the getAll() method:

$sessVariables = $this->_session->getAll();
$currentUser = $sessVariables['user'];
$basket = $sessVariables['basket'];

It is also possible to extract data from the session, i.e. to read and delete it from the session in a single call to the extract() method:

$value = $this->_session->extract('myVariable');

// is equivalent to
$value = $this->_session['myVariable'];
unset($this->_session['myVariable']);

// extract() can also take a default value
$value = $this->_session->extract('myVariable', 'default value');

5Writing data

To create or modify a session variable:

$this->_session['myVariable'] = $value;

If the value assigned to the variable is null, the variable is deleted from the session.

To read and write data, the session management object is used like an array. But this does not allow to write values directly to multi-dimensional arrays.
To modify a multidimensional array, you must first retrieve the complete array, modify it, then overwrite the session variable:

// it does not work
$this->_session['user']['name'] = 'Einstein';

// this is how to do it
$user = $this->_session['user'];
$user['name'] = 'Einstein';
$this->_session['user'] = $user;

6Deleting data

To destroy a session variable:

unset($this->_session['myVariable']);

To delete all the variables from the session:

$this->_session->clean();