phossa2/session
, (*1)
phossa2/session is a session library for PHP., (*2)
It requires PHP 5.4, supports PHP 7.0+ and HHVM. It is compliant with PSR-1,
PSR-2, PSR-3, PSR-4, and the proposed PSR-5., (*3)
Highlights
Installation
Install via the composer utility., (*9)
composer require "phossa2/session"
or add the following lines to your composer.json, (*10)
{
"require": {
"phossa2/session": "2.*"
}
}
Usage
Start the session, normally in your bootstrap file., (*11)
use Phossa2\Session\Session;
use Phossa2\Session\Carton;
// start a 'global' session
$sessGlobal = new Session('global');
// set 'global' session as the default
Carton::setDefaultSession($sessGlobal);
// start another 'private' session at the same time
$sessPrivate = new Session('private');
Then in your code using session data,, (*12)
// a box using default session 'global'
$boxGlobal = new Carton();
// global counter
++$boxGlobal['counter'];
// another box named 'toy' using the private session
$boxPrivate = new Carton('toy', $sessPrivate);
// private counter
++$boxPrivate['counter'];
Features
-
Multiple sessions supported, (*13)
Phossa2\Session\Session uses its own infra-structure. It can co-exists with
other session libs or PHP session. By default, it uses cookie as session id
exchange protocol. Different session then use different cookies., (*14)
// use a cookie named 'one'
$sessOne = new Session('one');
// use a cookie named 'two'
$sessTwo = new Session('two');
Close or destroy one session has no influence on other sessions., (*15)
-
Data seperation using Carton, (*16)
In PHP session, session data is stored in the global variable $_SESSION. It
provides storage only and there are no other utilities., (*17)
Phossa2\Session\Carton is a sandbox for data. User may name a carton box
instead of using the default name 'default'. Or even attach to a different
session instead of using the default session., (*18)
// box 1
$boxOne = new Carton('one', $sessPrivate);
$boxOne['drone'] = 2;
// box 2
$boxTwo = new Carton('two', $sessPrivate);
$boxTwo['drone'] = 1;
If either $name or $session is different, then the data is in different
namespaces., (*19)
By extending Phossa2\Session\Carton, user may even provide utilities, such
as data locking, usage monitoring, access control etc., (*20)
-
Extending the library, (*21)
phossa2/session refactors most of its dependents into seperate classes., (*22)
Handler is implementing the most widely adopted \SessionHandlerInterface
shipped in PHP. If no handler injected into session, it will utilize the
Phossa2\Session\Handler\FileHandler by default., (*23)
use Phossa2\Session\Handler\StorageHandler;
// inject a phossa2/storage handler
$session->setHandler(new StorageHandler($storage, '/tmp/session'));
-
Session id exchange protocol
Driver is implementing Phossa2\Session\Interfaces\DriverInterface. By
default, the Phossa2\Session\Driver\CookieDriver is used to set/get/del
session id on the client side by using cookies., (*24)
Users may write their own drivers to communicate with the client., (*25)
use My\Own\HeaderDriver;
// stores session id in `X-My-Own-Session` header
$session->setDriver(new HeaderDriver());
Multiple validators may be injected into session to do validation., (*26)
use Phossa2\Session\Validator\RemoteIp;
$session->addValidator(new RemoteIp());
By default, session id is generated by a built-in routine. User may use
his own generator such as using phossa2/uuid., (*27)
use Phossa2\Session\Generator\UuidGenerator;
$session->setGenerator(new UuidGenerator());
Middleware of this lib can be found in
phossa2/middleware, (*28)
Change log
Please see CHANGELOG from more information., (*29)
Testing
$ composer test
Contributing
Please see CONTRIBUTE for more information., (*30)
Dependencies
License
MIT License, (*33)