Skip to content


Using the session for your own data

Kirby now has a global session object that you can access with $kirby->session().

$session = $kirby->session();

You can do all sorts of cool stuff with it:

Getting and setting

$session->get('myplugin.mykey', 'my default value');
$session->set('myplugin.mykey', 'my new value');


$session->set('myplugin.counter', 10);
$session->increment('myplugin.counter', 2, 50);
$session->get('myplugin.counter') // 12

The third param to $session->increment() is the maximum value; the value won't be incremented beyond that value.


$session->set('myplugin.counter', 10);
$session->decrement('myplugin.counter', 2, 0);
$session->get('myplugin.counter') // 8

The third param to $session->decrement() is the minimum value; the value won't be decremented beyond that value.

Pulling data out of the session

$session->pull('myplugin.message', 'my default value');

$session->pull() removes the value from the session after getting it.

Removing data


Clearing all session data


Destroying the session completely


Interesting stats


// total duration between start and expiry time in seconds

// seconds of inactivity after which the session will expire

Requesting a long session

If you want to use a "long" session (a session with a long duration and without timeout) in your controllers and templates, you can request it like this:

$session = $kirby->session(['long' => true]);

If a session already exists, it is automatically extended appropriately, so you don't have to deal with any edgecases.

Manually collecting garbage

Kirby periodically deletes expired sessions (so-called "garbage collection").

If your site has lots of users or if you create sessions for your site visitors, it might make sense to clean up expired sessions in the background to increase performance.

First you need to disable the automatic garbage collector in your Kirby config:


return [
    'session' => [
        'gcInterval' => false

Now you can write a script with the following contents and periodically run it with a Cronjob (depending on the amount of sessions every few minutes to every few hours):


require 'path/to/kirby/bootstrap.php';

$kirby = new Kirby();


You can change the session settings with the session option in your /site/config/config.php.

All session options…