PHP itself provides session management. For this reason the super global variable $_SESSION is used to store the session data and several functions from the APF\core\session package allow to start, create or delete a session. To have a tool for transparent session management some more code must be added to use the session commonly within several program parts. This issue is solved by the Session of the Adventure PHP Framework.

If a session is needed within an application the session can be initialized by

PHP code
use APF\core\session\Session; $session = new Session('{namespace}');

{namespace} thereby describes the namespace of the session, that should be used in further code parts. Commonly the namespace is filled with the namespace of the current application to indicate affiliation.

The Session partitions the session provided by PHP natively into different namespaces in order to use on PHP session multiple times by Adventure PHP Framework applications at the same time.

PHP code
$session->save('MyParam', 'MyValue');

makes it able to store data in the session namespace defined by instantiating the session. Besides, the Session makes sure that the session is always existent while using it in an application. session_start() is not necessary any more. Data stored in a session namespace can easily gathered by

PHP code
echo $session->load('MyParam');

The data displayed in this example is read from the namespace the session was initialized with. To initiate a logout procedure all data within a specific namespace can be deleted by

PHP code

Afterwards calling load() returns null. Calling

PHP code

returns an associative array including all entries registered in the session namespace given when creating the session. In case you only need a list of keys registered within the session, you can call

PHP code

In case you intend to remove content from the session you can use delete():

PHP code


Do you want to add a comment to the article above, or do you want to post additional hints? So please click here. Comments already posted can be found below.
There are no comments belonging to this article.

In order to provide a state-of-the-art web experience and to continuously improve our services we are using cookies. By using this web page you agree to the use of cookies. For more information, please refer to our Privacy policy.