Skip to content
A session handler for PHP
Branch: master
Clone or download
Latest commit b7d6f8f Mar 21, 2019
Permalink
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
.editorconfig
.gitattributes
.gitignore
.scrutinizer.yml Fixed build Mar 7, 2019
.travis.yml
LICENSE Fixed cs Mar 7, 2019
README.md
_config.yml
build.xml Fixed cs Mar 7, 2019
composer.json Add session middleware Mar 21, 2019
phpstan.neon
phpunit.xml

README.md

Session handler

A session handler for PHP

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

Requirements

  • PHP 7.2+

Installation

composer require odan/session

Usage

use Odan\Session\PhpSession;

// Set session options before we start
// You can use all the standard PHP session configuration options
// https://secure.php.net/manual/en/session.configuration.php

$session->setOptions([
    '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
$session->start();

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

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

// Optional: Force the session to be saved and closed
$session->save();

Methods

// 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
$session->remove('key');

// Clear all session variables
$session->clear();

// Generate a new session ID
$session->regenerateId();

// Clears all session data and regenerates session ID
$session->destroy();

// Get the current session ID
$session->getId();

// Set the session ID
$session->setId('...');

// Get the session name
$session->getName();

// Set the session name
$session->setName('my-app');

// Returns true if the attribute exists
$session->has('foo');

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

// Get the number of values.
$session->count();

// Force the session to be saved and closed
$session->save();

// Set session runtime configuration
// All supported keys: http://php.net/manual/en/session.configuration.php
$session->setOptions($options);

// Get session runtime configuration
$session->getOptions();

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

// Get cookie parameters
$session->getCookieParams();

Adapter

PHP Session

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

Example:

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

Integration

Slim 3 framework integration

Configuration

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');
    $session->setOptions($settings['session']);
    
    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:

$app->add(\Odan\Session\SessionMiddleware::class);

Register middleware for a single route:

$this->get('/', \App\Action\HomeIndexAction::class)
    ->add(\Odan\Session\SessionMiddleware::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);
})->add(\Odan\Session\SessionMiddleware::class);
You can’t perform that action at this time.