1. Go to this page and download the library: Download tobento/service-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/ */
tobento / service-session example snippets
use Tobento\Service\Session\Session;
use Tobento\Service\Session\SessionInterface;
use Tobento\Service\Session\SaveHandlerInterface;
use Tobento\Service\Session\ValidationInterface;
$session = new Session(
name: 'sess',
maxlifetime: 1800,
cookiePath: '/',
cookieDomain: '',
cookieSamesite: 'Strict',
secure: true,
httpOnly: true,
saveHandler: null, // null|SaveHandlerInterface
validation: null, // null|ValidationInterface
);
var_dump($session instanceof SessionInterface);
// bool(true)
use Tobento\Service\Session\SessionFactory;
use Tobento\Service\Session\SessionFactoryInterface;
use Tobento\Service\Session\SessionInterface;
use Tobento\Service\Session\SaveHandlerInterface;
use Tobento\Service\Session\ValidationInterface;
$sessionFactory = new SessionFactory();
$session = $sessionFactory->createSession('name', [
'maxlifetime' => 1800,
'cookiePath' => '/',
'cookieDomain' => '',
'cookieSamesite' => 'Strict',
'secure' => true,
'httpOnly' => true,
'saveHandler' => null, // null|SaveHandlerInterface
'validation' => null, // null|ValidationInterface
]);
var_dump($session instanceof SessionInterface);
// bool(true)
use Tobento\Service\Session\PdoMySqlSaveHandler;
use Tobento\Service\Session\SaveHandlerInterface;
use PDO;
$pdo = new PDO(
dsn: 'mysql:host=localhost;dbname=db_name',
username: 'root',
password: '',
options: [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => false,
],
);
$saveHandler = new PdoMySqlSaveHandler(
table: 'session',
pdo: $pdo
);
var_dump($saveHandler instanceof SaveHandlerInterface);
// bool(true)
use Tobento\Service\Session\NullSaveHandler;
use Tobento\Service\Session\SaveHandlerInterface;
$saveHandler = new NullSaveHandler();
var_dump($saveHandler instanceof SaveHandlerInterface);
// bool(true)
use Tobento\Service\Session\HttpUserAgentValidation;
use Tobento\Service\Session\ValidationInterface;
$validation = new HttpUserAgentValidation(
httpUserAgent: $_SERVER['HTTP_USER_AGENT'] ?? null,
httpUserAgentKey: '_session_httpUserAgent' // default
);
var_dump($validation instanceof ValidationInterface);
// bool(true)
var_dump($validation->httpUserAgent());
// string(78) "Mozilla/5.0 ..."
use Tobento\Service\Session\Validations;
use Tobento\Service\Session\RemoteAddrValidation;
use Tobento\Service\Session\HttpUserAgentValidation;
use Tobento\Service\Session\ValidationInterface;
$validation = new Validations(
new RemoteAddrValidation($_SERVER['REMOTE_ADDR'] ?? null),
new HttpUserAgentValidation($_SERVER['HTTP_USER_AGENT'] ?? null),
);
var_dump($validation instanceof ValidationInterface);
// bool(true)
$validations = $validation->validations();
// array<int, ValidationInterface>
use Tobento\Service\Session\ValidationInterface;
use Tobento\Service\Session\SessionInterface;
use Tobento\Service\Session\SessionValidationException;
/**
* ValidationInterface
*/
interface ValidationInterface
{
/**
* Process the validation.
*
* @param SessionInterface $session
* @return void
* @throws SessionValidationException
*/
public function process(SessionInterface $session): void;
}
use Tobento\Service\Session\SessionStartException;
use Tobento\Service\Session\SessionExpiredException;
use Tobento\Service\Session\SessionValidationException;
try {
$session->start();
} catch (SessionStartException $e) {
// handle
} catch (SessionExpiredException $e) {
$session->destroy();
// You might to restart session and regenerate id
// on the current request.
$session->start();
$session->regenerateId();
// Or you might send a message to the user instead.
} catch (SessionValidationException $e) {
// handle
}
$session->set('key', 'value');
// using dot notation:
$session->set('meta.color', 'color');
$value = $session->get('key');
// using dot notation:
$value = $session->get('meta.color');
// using a default value if key does not exist
$value = $session->get('key', 'default');