ghostff/session

PHP Session Manager (non-blocking, flash, segment, session encryption).

v1.03.0 2017-10-15 00:31 UTC

This package is auto-updated.

Last update: 2020-05-16 00:50:27 UTC


README

PHP Session Manager (non-blocking, flash, segment, session encryption). Uses PHP open_ssl for optional encrypt/decryption of session data.

Driver support Scope

file   cookie   pdo   memcached   redis   license   Minimum PHP Version

Installation

You can download the Latest release version as a standalone, alternatively you can use Composer

$ composer require ghostff/session
"require": {
    "ghostff/session": "^1.0"
}

Basic usage:

Registering Error Handler (optional)

#This method must be implemented before Session::start
Session::registerErrorHandler(function(string $error, int $error_code)
{
    #Debug::Log($error);
});

Setting or getting session id :void

#When setting ID, method must be implemented before Session::start
Session::id(bin2hex(random_bytes(32)));

#Get ID
echo Session::id();

Initializing Session

$session = Session::start($optional_session_namespace);

Using Segment :Segment

 $segment = $session->segment($required_segment_name);

Setting Session Data

$session->name = 'foo';
# Setting Segment
$segment->name = 'bar';

# Setting Flash
$session->flash->name = 'foobar';
# Setting Segment Flash
$segment->flash->name = 'barfoo';

$session->commit();

Retrieving Session Data

echo $session->name; # outputs foo
# Retrieving Segment
echo $segment->name; # outputs bar

# Retrieving Flash
echo $session->flash->name; # outputs foobar
# Retrieving Segment Flash
echo $segment->flash->name; # outputs barfoo

Removing Session Data

$session->remove->name;
# Removing Segment
$segment->remove->name;

# Removing Flash
$session->remove->flash->name;
# Removing Segment Flash
$segment->remove->flash->name;

Retrieve all session or segment data :array

$session->all($optional_segment);

Check if variable exist in current session namespace :bool

$session->exist($variable_name, $option_segment, $in_flash);

Removing active session or segment data :void

$session->clear($optional_segment);

Destroying session :void

$session->destroy();

Regenerate session ID :void

$session->rotate($keep_old_session_data);

Change Log v1.02.0

Initializing Session

A new optional argument($auto_save: true) was added to the start method.

$session = Session::start($optional_session_namespace, $auto_save);

Which allows uncommitted "forgot to commit" changes to saves automatically. If set to false, uncommitted changes will be discarded.

Change Log v1.03.0

  • start method now accepts null arg as namespace.
  • Default session driver is now set to file.
  • Auto delete session after rotate is now defaulted to false.
  • A setConfigPath method has been added.
#This method must be implemented before Session::start
Session::setConfigPath('my/config/path/config.php');
  • A set queue has been added
$session->name = 'foo';
$session->name = 'foo1';

var_dump($session->name); # Outputs 'foo1'

$session->name('foo')
$session->name('foo1');

var_dump($session->name); # Outputs ['foo', 'foo1'];

# Array
$session->name = ['foo', 'bar', ...];
# is same as
$session->name('foo', 'bar', ...);

When flash are placed using the new queue method, they will be dispatched one after another on each request

$session->flash->message('invalid 1', 'invalid 2');

With the above invalid 1 will be dispatched on first load/reload and invalid 2 on second.