PHP code example of bayfrontmedia / session-manager

1. Go to this page and download the library: Download bayfrontmedia/session-manager 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/ */

    

bayfrontmedia / session-manager example snippets


use Bayfront\SessionManager\Handlers\LocalHandler;

$handler = new LocalHandler('/root_path');

use Bayfront\SessionManager\Handlers\PdoHandler;

$dbh = new PDO('mysql:host=localhost;dbname=DATABASE_NAME', 'USERNAME', 'PASSWORD');

// Pass the table name to be used in the constructor - "sessions" by default
$handler = new PdoHandler($dbh, 'sessions');

try {
    $handler->up();
} catch (HandlerException $e) {
    die($e->getMessage());
}

$client = new Client([
    'scheme' => 'tcp',
    'host' => '10.0.0.1',
    'port' => 6379,
    'tcp_nodelay' => true,
    'persistent' => true,
    'username' => 'USERNAME',
    'password' => 'PASSWORD'
]);


$handler = new PredisHandler($client, 3600, 'prod:session:');

$redis = new Redis();

$redis->pconnect('10.0.0.1', 6379, 2, 'persist_id');

$redis->auth([
    'USERNAME',
    'PASSWORD'
]);

$redis->setOption(Redis::OPT_PREFIX, 'global:prefix:');

$handler = new RedisHandler($redis, 3600, 'prod:session:');

use Bayfront\SessionManager\Handlers\LocalHandler;
use Bayfront\SessionManager\Session;

$handler = new LocalHandler('/root_path');

$config = [
    'cookie_name' => 'bfm_sess',
    'cookie_path' => '/',
    'cookie_domain' => '',
    'cookie_secure' => true,
    'cookie_http_only' => true,
    'cookie_same_site' => 'Lax', // None, Lax or Strict
    'sess_regenerate_duration' => 300, // 0 to disable
    'sess_lifetime' => 3600, // 0 for "until the browser is closed"
    'sess_gc_probability' => 1, // 0 to disable garbage collection
    'sess_gc_divisor' => 100
];

$session = new Session($handler, $config);

$session->start();

$session->startNew();

$session->regenerate();

$session->destroy();

echo $session->getId();

echo $session->getLastActive();

echo $session->getLastRegenerate();

echo $session->get('user.id');

if ($session->has('user.id')) {
    // Do something
}

$session->set('user.id', 5);

$session->forget('user.id');

$session->flash('status', 'Task was successful');

echo $session->getFlash('status');

if ($session->hasFlash('status')) {
    // Do something
}

$session->keepFlash([
    'status'
]);

$session->reflash();