Skip to content
A session handler for PHP
Branch: master
Clone or download
Latest commit b7d6f8f Mar 21, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
src/Session Add session middleware Mar 21, 2019
tests Add session middleware Mar 21, 2019
.cs.php Fixed cs Mar 7, 2019
.scrutinizer.yml Fixed build Mar 7, 2019
LICENSE Fixed cs Mar 7, 2019
build.xml Fixed cs Mar 7, 2019
composer.json Add session middleware Mar 21, 2019

Session handler

A session handler for PHP

Latest Version on Packagist Software License Build Status Code Coverage Scrutinizer Code Quality Total Downloads


  • PHP 7.2+


composer require odan/session


use Odan\Session\PhpSession;

// Set session options before we start
// You can use all the standard PHP session configuration options

    'name' => 'app',
    // turn off automatic sending of cache headers entirely
    'cache_limiter' => '',
    // garbage collection
    'gc_probability' => 1,
    'gc_divisor' => 1,
    'gc_maxlifetime' => 30 * 24 * 60 * 60,
    // security on
    'cookie_httponly' => true,
    'cookie_secure' => true,

// Create a standard session hanndler
$session = new PhpSession();

// Start the session

// Set session value
$session->set('bar', 'foo');

// Get session value
echo $session->get('bar'); // foo

// Optional: Force the session to be saved and closed


// Get session variable
$foo = $session->get('foo');

// Get session variable or the default value
$bar = $session->get('bar') ?? 'my default value';

// Set session variable
$session->set('bar', 'that');

// Get all session variables
$all = $session->all();

// Delete a session variable

// Clear all session variables

// Generate a new session ID

// Clears all session data and regenerates session ID

// Get the current session ID

// Set the session ID

// Get the session name

// Set the session name

// Returns true if the attribute exists

// Sets multiple values at once
$session->replace(['foo' => 'value1', 'bar' => 'value2']);

// Get the number of values.

// Force the session to be saved and closed

// Set session runtime configuration
// All supported keys:

// Get session runtime configuration

// Set cookie parameters
$session->setCookieParams(4200, '/', '', false, false);

// Get cookie parameters


PHP Session

  • The default PHP session handler
  • Uses the native PHP session functions


use Odan\Session\PhpSession;

$session = new PhpSession();

Memory Session

  • Optimized for integration tests (with phpunit)
  • Prevent output buffer issues
  • Run sessions only in memory
use Odan\Session\MemorySession;

$session = new MemorySession();


Slim 3 framework integration


Add your application-specific settings.

These are stored in the settings configuration key of Slim.

// Session
$config['session'] = [
    'name' => 'webapp',
    'cache_expire' => 0,
    'cookie_httponly' => true,
    'cookie_secure' => true,

Add the session factory:

use Odan\Session\PhpSession;
use Odan\Session\SessionInterface;
use Odan\Session\SessionMiddleware;
use Psr\Container\ContainerInterface as Container;

$container[SessionInterface::class] = function (Container $container) {
    $session = new PhpSession();
    // Optional settings
    $settings = $container->get('settings');
    return $session;

Middleware setup

Add the middleware factory:

$container[SessionMiddleware::class] = function (Container $container) {
    return new SessionMiddleware($container->get(SessionInterface::class));

Add the Slim application middleware

Register middleware for all routes:


Register middleware for a single route:

$this->get('/', \App\Action\HomeIndexAction::class)

Register the middleware for a group of routes:

$app->group('/users', function () {
    $this->post('/login', \App\Action\UserLoginSubmitAction::class);
    $this->get('/login', \App\Action\UserLoginIndexAction::class);
    $this->get('/logout', \App\Action\UserLogoutAction::class);
You can’t perform that action at this time.