1. Go to this page and download the library: Download ddrv/slim-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/ */
ddrv / slim-session example snippets
$storage = new Ddrv\Slim\Session\Storage\FileStorage('/path/to/sessions', 'sess_id');
$handler = new Ddrv\Slim\Session\Handler($storage);
/** @var Ddrv\Slim\Session\Handler $handler */
$sessionId = array_key_exists('sess_id', $_COOKIE) ? $_COOKIE['sess_id'] : $handler->generateId();
$session = $handler->read($sessionId);
// some logic
// When you need to update session ID do
$session->regenerate();
// some logic
if ($session->isNeedRegenerate()) {
$handler->destroy($sessionId);
$sessionId = $handler->generateId();
}
$handler->write($sessionId, $session); // store data to storage and close session
// add session cookie to response
setcookie('sess_id', $sessionId, time() + 86400, '/', '.example.com', false, true);
/** @var Ddrv\Slim\Session\Session $session */
$session->flash('key1', 'value'); // 'key1' will be stored only for the current and the next request
// Current request
$session->has('key1'); // true
$value = $session->get('key1'); // 'value'
// Next request
$session->has('key1'); // true
$value = $session->get('key1'); // 'value'
// Other request
$session->has('key1'); // false
$value = $session->get('key1'); // null