[Since 0.5.3]


Pletfix's session is an simple adapter for the PHP Session.

Pletfix starts the session automatically and ends it at soon as possible. Other scripts that share the same session are not blocked longer than necessary.

See also the interesting blog post [](PHP Session Locking) on the topic of


Pletfix stores the session files in the storage/sessions directory by default. To configure the Pletfix Session, you should modify the session option in your config/session.php configuration file.

Basic Usage

You can get an instance of the Session from the Dependency Injector:

/** @var \Core\Services\Contracts\Session $session */
$session = DI::getInstance()->get('session');

You can also use the global session() function to get the Session, it is more comfortable:

$session = session();

Note, you can only write data to the cookie-based session as long as nothing has been sent to the browser. This also applies to the first read access, because the response header will be changed then.

Read Data

The get method reads the data from the session:

session()->get('foo', 'default');

Write Data

The set method writes the data to the session:

session()->set('foo', $foo);

Write needs an exclusive lock for the session. Therefore, if the session was not started explicitly, the set method opens the session first and commits it immediately after writing so that other scripts do not block.

To prevent the session from being started and committed for every single write operation, you should start the session explicitly if several values are stored:

    ->set('foo', $foo)
    ->set('bar', $bar)

However, you can also use a closure that start and commit the session for you:

session(function(Session $session) use ($foo, $bar) {
    $session->set('foo', $foo);
    $session->set('bar', $bar);

Available Methods

The Session object has these methods:

Method Listing


The cancel discards the changes made during the current request.


This function calls session_abort() internally.


The clear method removes all items from the session:


See also delete.


To save the session data and end its use during the current request, call the commit method:


The Session will automatically commit the changes when PHP is finished executing a script.


The delete method removes an entry from the session:


See also clear.


The get method is used to retrieve items from the sesion. If the item does not exist in the session, null will be returned.

$value = session()->get('key');

If you wish, you may pass a second argument to the get method specifying the default value you wish to be returned if the item doesn't exist:

$value = session()->get('key', 'default');

There is also a shorter way to get a session value:

 session('key', 'default');


The has method may be used to determine if an item exists in the session:

if (session()->has('key')) {


The isStarted method determines if the session is started and therefore writable.

$isStarted = session()->isStarted(); 


The kill method removes the session completely (both values as well as session cookie).



This method will pass a closure for writing session data. The session is started and committed automatically.

session()->lock(function(Session $session) use ($foo, $bar) {
    $session->set('foo', $foo);
    $session->set('bar', $bar);


Any time a user has a change in privilege be sure to regenerate the session id:



The set method may be used to store an item in the session:

session()->set($key, $value);


The set method starts new or resume existing session:


(edit on GitHub)