Awesome Open Source
Awesome Open Source

Session PHP(7+)

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

# Start session with default default or specified configurations.
$session = new Session(); 

$session->set('email', '[email protected]');

echo $session->get('email');

Configuration (optional)

# loads the default configuration "src/Session/default_config.php"
Configuration::loadDefaultConfiguration();
 
# use custom configuration file.
Configuration::loadDefaultConfiguration('path/to/my/config.php');
 
# overriding specific configuration settings
Configuration::set(['encrypt_data' => true]);

Initializing Session

# Start session with default default or specified configurations.
$session = new Session(); 

$session = new Session(Configuration::set(['encrypt_data' => true]));

# Start session with custom ID
$session = new Session(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 foo
echo $session->getOrDefault('unset_value', 'not found'); # outputs not found
# Retrieving Segment
echo $segment->get('name'); # outputs bar
echo $segment->getOrDefault('unset_value', 'not found'); # outputs not found

# Retrieving Flash
echo $session->getFlash('name'); # outputs foobar
echo $session->getFlashOrDefault('name', 'not found'); # outputs not found
# Retrieving Segment Flash
echo $segment->getFlash('name'); # outputs barfoo
echo $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 Queued Session Data :mixed

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

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
Php (404,388
Mysql (12,268
Redis (6,737
Php7 (4,469
Encryption (2,698
Session (584
Memcached (434
Flash (434
Non Blocking (176
Session Management (166
Related Projects