Session handling 3.2

From Cloudrexx Development Wiki
Jump to: navigation, search

Contrexx has its own session handler in order to be independent from server configuration.

In backend mode, session is initialized automaticly, but in frontend, the session class is only loaded if necessary (user login, upload, etc.). To load the session in frontend mode, use the following code:

Introduction

Due to some additional functionality of the Contrexx session, the $_SESSION variable contains a PHP object which is a recursive array.

The session handling has been rewritten because it had to be possible to lock the session values one by one. The database has been changed too, there is a new database table session_variable which contains all values. In this table we also have a foreign key named parent_id which is a reference to the id field of the same table. This means it is possible to store data recursively in session_variable table.


Note: This documentation refers to version 3.2 and 4. For newer versions of Cloudrexx, please refer to Session handling.

Initialization

\cmsSession::getInstance(); 

Checking whether the session is initialized

You can check whether the session is initialized or not. This method is called by getInstance() too, you don't have to think about this to initialize the session, but maybe you need this for other purposes:

if (\cmsSession::isInitialized()) {}

Important information for usage

The key length (in example 'this_is_the_key') of a session value cannot be longer than 40 chars. This length is given through the length of the database field for the key.

$_SESSION['this_is_the_key'] = 'value';

toArray() method

In special cases it is necessary to explicitly call the method toArray() on the $_SESSION object.

This is an example:

foreach (array_keys($_SESSION[self::$sesskey]) as $key) {}

this won't work. It should look like this:

foreach (array_keys($_SESSION[self::$sesskey]->toArray()) as $key) {}

API

The contrexx API documentation has more info about the session class.