Skip to content

Repository files navigation

Session PHP(7.4+)

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

Driver Support Scope

filecookiemysqlsqlitememcachedredislicenseMinimum PHP Version

Installation

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

composer require ghostff/session

Basic usage

# Start session with default configurations.$session = newSession(); $session->set('email', 'foo@bar.com');
echo$session->get('email');

Configuration Options

# use custom configuration file.
Session::setConfigurationFile('path/to/my/config.php');
# overriding specific configuration settings
Session::updateConfiguration([
Session::CONFIG_DRIVER => Redis::class,
Session::CONFIG_START_OPTIONS => [
Session::CONFIG_START_OPTIONS_SAVE_PATH => __DIR__ . '/tmp'
]
]);
# override a configuration for current session instance$session = newSession([Session::CONFIG_ENCRYPT_DATA => true]);

Initializing Session

# Start session with an auto generated id.$session = newSession(); # Start session with custom ID$session = newSession(null, bin2hex(random_bytes(32)));

Using Segment :Session

$segment = $session->segment('my_segment');

Retrieving Session ID :string

echo$session->id();

Committing changes :void

# Opens, writes and closes session.$session->commit();

Setting Session Data :Session

$session->set('fname', 'foo');
# Setting Segment$segment->set('name', 'bar');
# Setting Flash$session->setFlash('name', 'foobar');
# Setting Segment Flash$segment->setFlash('name', 'barfoo');
$session->commit();

Retrieving Session Data :mixed

echo$session->get('name'); # outputs fooecho$session->getOrDefault('unset_value', 'not found'); # outputs not found# Retrieving Segmentecho$segment->get('name'); # outputs barecho$segment->getOrDefault('unset_value', 'not found'); # outputs not found# Retrieving Flashecho$session->getFlash('name'); # outputs foobarecho$session->getFlashOrDefault('name', 'not found'); # outputs not found# Retrieving Segment Flashecho$segment->getFlash('name'); # outputs barfooecho$segment->getFlashOrDefault('name', 'not found'); # outputs not found

Removing Session Data :Session

$session->del('name');
# Removing Segment$segment->del('name');
# Removing Flash$session->delFlash('name');
# Removing Segment Flash$segment->delFlash('name');

Retrieve all session or segment data :array

$session->getAll();
# Retrieve only in specified segment.$session->getAll('my_segment_name');

Check if variable exist in current session namespace :bool

$session->exist('name');
# Search flashes$session->exist('name', true);

Removing all data in current segment :Session

$session->clear();

Destroying session :void

$session->destroy();

Regenerate session ID :void

$session->rotate();
# Delete the old associated session file or not$session->rotate(true);

Setting Queued Session Data :Session

$session->push('age', 10)
->push('age', 20)
->push('age', 30)
->push('age', 40);

Retrieving (pop/shift) Queued Session Data :mixed

echo$session->pop('age', true); # outputs 10echo$session->pop('age', true); # outputs 20echo$session->pop('age'); # outputs 40echo$session->pop('age'); # outputs 30

Driver configuration

You can provide your own database connection (PDO, Memcached, Redis, etc). Provide the connection before constructing the session:

// Example: supplying a PDO connection$pdo = newPDO('mysql:host=127.0.0.1;dbname=sessions', 'user', 'pass', [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);
Session::setConnection(Session::CONFIG_MYSQL_DS, $pdo);
// Then construct your session (the configured driver will use this connection)$session = newSession();

Similarly, you can set connections for:

  • Session::CONFIG_SQLITE_DS with a PDO SQLite connection
  • Session::CONFIG_MEMCACHED_DS with a Memcached instance
  • Session::CONFIG_REDIS_DS with a Redis instance

About

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

Topics

Resources

Stars

37 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages