PHP code example of lukman-ss / session

1. Go to this page and download the library: Download lukman-ss/session library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lukman-ss / session example snippets


use Lukman\Session\Handlers\ArraySessionHandler;

$handler = new ArraySessionHandler();

use Lukman\Session\Handlers\FileSessionHandler;

// Automatically creates the directory if it doesn't exist
$handler = new FileSessionHandler('/path/to/sessions');

use Lukman\Session\Handlers\ArraySessionHandler;
use Lukman\Session\SessionStore;
use Lukman\Session\SessionIdGenerator;

$handler = new ArraySessionHandler();
$store = new SessionStore($handler, new SessionIdGenerator());

// Start the session (loads data from the handler, generates new ID if needed)
$store->start();

// Check and save session values
$store->put('user_id', 42);

if ($store->has('user_id')) {
    $userId = $store->get('user_id'); // 42
}

// Persist the changes to the storage handler
$store->save();

$store->start();

// Automatically creates nested arrays
$store->put('user.profile.name', 'Lukman');
$store->put('user.profile.role', 'Administrator');

// Retrieve nested values
$name = $store->get('user.profile.name'); // 'Lukman'

// Check existence
if ($store->has('user.profile.role')) {
    // forget nested keys
    $store->forget('user.profile.role');
}

$store->start();

// Set flash data for the next request
$store->flash('status', 'Profile updated successfully!');

// Set flash data only for the current request
$store->now('info', 'Reading log file...');

// Age flash data (typically run at the end of the request/response cycle)
// - Removes old flash data
// - Marks new flash data as old
$store->ageFlashData();

// Keep specific flash data or reflash all
$store->keep('status');
$store->reflash();

$store->start();

// Get the current token, or automatically generate one if not present
$token = $store->token();

// Forcefully regenerate the CSRF token
$newToken = $store->regenerateToken();

$store->start();

// Regenerate the session ID keeping all data (optionally destroy the old session in handler)
$store->regenerate(true);

// Destroy the current session in storage
$store->destroy();

// Flush all data, destroy current session, and regenerate ID (log out)
$store->invalidate();

use Lukman\Session\SessionManager;

$config = [
    'driver'   => 'file',
    'lifetime' => 120, // in minutes (automatically converted to 7200 seconds TTL)
    'files'    => __DIR__ . '/sessions',
];

$manager = new SessionManager($config);

// Get the default store
$store = $manager->store();
$store->start();

// Access specific driver stores
$arrayStore = $manager->driver('array');