Sessions

[Since 0.5.3]

Introduction

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 [https://ma.ttias.be/php-session-locking-prevent-sessions-blocking-in-requests/](PHP Session Locking) on the topic of ma.ttias.be.

Configuration

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:

session()
    ->start()
    ->set('foo', $foo)
    ->set('bar', $bar)
    ->commit();

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

abort()

The cancel discards the changes made during the current request.

session()->cancel();

This function calls session_abort() internally.

clear()

The clear method removes all items from the session:

session()->clear();

See also delete.

commit()

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

session()->commit();

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

delete()

The delete method removes an entry from the session:

session()->delete('key');

See also clear.

get()

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');

has()

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

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

isStarted()

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

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

kill()

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

session()->kill(); 

lock()

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);
});

regenerate()

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

session()->regenerate();

set()

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

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

start()

The set method starts new or resume existing session:

session()->start();

(edit on GitHub)