2017 © Pedro Peláez
 

library cesession

Alternative PHP session handler for Monolyth unframework

image

monolyth/cesession

Alternative PHP session handler for Monolyth unframework

  • Thursday, March 30, 2017
  • by monomelodies
  • Repository
  • 1 Watchers
  • 0 Stars
  • 146 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 13 Versions
  • 3 % Grown

The README.md

CeSession

Alternative PHP session handler for Monolyth unframework, (*1)

Who is this for?

While PHP supports session handling, the implementation is less than stellar. Cesession provides an alternative with built-in helpers to use either an SQL database, memcached or a NoSQL database., (*2)

Cesession offers a seamless interface with the well-known $_SESSION superglobal, so any existing code should Just Work(tm)., (*3)

An added advantage of using SQL based databases is that you can set up a foreign key constraint between the current user and the current session, so people get automatically logged out., (*4)

Installation

$ composer require monolyth/cesession, (*5)

Manual

  1. Clone or download the repository;
  2. Add the Monolyth\Cesession namespace to your autoloader for /path/to/cesession/src.
  3. Create the relevant table (see scripts in ./info/sql)

That's it!, (*6)

Setting up

To begin, before any call to session_start create the Monolyth\Cesession\Session object and register a handler. Currently Cesession ships with a Pdo handler that does exactly what its name implies (store the session data in a PDO-compatible database, e.g. PostgreSQL or MySQL):, (*7)

<?php

use Monolyth\Cesession\Session;
use Monolyth\Cesession\Handler;

$session = new Session('my-session-name');
$db = new PDO('dsn', 'user', 'pass');
$handler = new Handler\Pdo($db);
$session->registerHandler($handler);
session_start();

Alternatively, there is also a Memcached handler. Since objects stored in Memcached can be deleted at any time, this should only be used as a fallback in conjunction with a more persistent handler like Pdo., (*8)

Database tables

Example schemas are included in the ./info directory. These contain the minimum columns needed for the Pdo handler to work; depending on your needs you can add extra columns (e.g. auth for the currently logged in user's id)., (*9)

Registering handlers

To register a handler, simply call the registerHandler method on the $session object and pass in a handler object. Each handler object must implement the Monolyth\Cesession\Handler interface. This is done both for type hinting and to ensure the required methods exist. The Handler interface is a subset of PHP's built-in SessionHandlerInterface., (*10)

The optional second argument to registerHandler is a probability percentage between 0 and 100. This signifies the probability that for supporting calls, the action is also delegated to the next handler (if defined)., (*11)

E.g., say you want to store sessions in Memcached (fast!) but persist to a PDO backend every ten calls on average:, (*12)

<?php

$session->registerHandler(new Handler\Memcached($memcached), 10);
$session->registerHandler(new Handler\Pdo($db));

Note: currently only the Pdo and Memcached handlers are supported out of the box., (*13)

Forcing an operation on all handlers

Sometimes you'll want to ensure an operation gets persisted to all handlers, for instance when a user's authentication state changes. Use the force method for this. The first argument is the session method you need to call, the second an array of arguments to pass:, (*14)

<?php

// Force emptying of the current session on all handlers:
$session->force(
    'write',
    [session_id(), ['data' => serialize([])] + $session::$session]
);

Internally this calls the method on all defined handlers with a probability of 100%. Note that using force only makes sense if you have multiple handlers defined with varying probabilities., (*15)

The forwarding is done directly on the handlers, hence the arguments passed are slightly different than on the main Session object. Most importantly, $data is not passed to write as a string but as a hash with augmented meta information about the session., (*16)

Writing your own handlers

See the examples in ./src/Handler. It's simple enough., (*17)

Session encoding/decoding

By default, Cesession uses the session.serialize_handler PHP ini setting to en/decode session data. You can override this, e.g. in your php.ini or by calling ini_set('session.serialize_handler, 'new value'). See the PHP manual for valid values, but say you wanted to store session data using regular serialize and unserialize calls, you would do this:, (*18)

ini_set('session.serialize_handler', 'php_serialize');

This allows you to modify the session data more easily from other places in your code (say, a cronjob) without having to resort to weird trickery., (*19)

The Versions

30/03 2017

dev-master

9999999-dev

Alternative PHP session handler for Monolyth unframework

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php session handler monolyth

30/03 2017

1.1.3

1.1.3.0

Alternative PHP session handler for Monolyth unframework

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php session handler monolyth

20/08 2016

1.1.2

1.1.2.0

Alternative PHP session handler for Monolyth unframework

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php session handler monolyth

20/08 2016

dev-develop

dev-develop

Alternative PHP session handler for Monolyth unframework

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php session handler monolyth

17/07 2016

1.1.1

1.1.1.0

Alternative PHP session handler for Monolyth unframework

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php session handler monolyth

10/07 2016

1.1.0

1.1.0.0

Alternative PHP session handler for Monolyth unframework

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php session handler monolyth

04/06 2016

1.0.3

1.0.3.0

Alternative PHP session handler

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php session handler

12/03 2016

1.0.2

1.0.2.0

Alternative PHP session handler

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php session handler

10/03 2016

1.0.1

1.0.1.0

Alternative PHP session handler

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php session handler

02/10 2015

1.0.0

1.0.0.0

Alternative PHP session handler

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php session handler

29/09 2015

0.0.3

0.0.3.0

Alternative PHP session handler

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php session handler

06/09 2015

0.0.2

0.0.2.0

Alternative PHP session handler

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php session handler

24/08 2015

0.0.1

0.0.1.0

Alternative PHP session handler

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php session handler