-
Notifications
You must be signed in to change notification settings - Fork 0
Quick Start
A five-minute tour of the public API. Every snippet runs as-is once you have
required vendor/autoload.php.
Call createImmutable() once per request, then start():
use InitPHP\Sessions\Session;
Session::createImmutable()
->start();With no adapter, PHP's default save handler is used. To store sessions elsewhere, pass an adapter — see Adapters:
use InitPHP\Sessions\Adapters\FileAdapter;
Session::createImmutable(new FileAdapter(['path' => __DIR__ . '/storage']))
->start();set() is chainable; push() returns the value you just stored:
Session::set('username', 'admin')
->set('role', 'editor');
$token = Session::push('csrf', bin2hex(random_bytes(16)));Session::get('username'); // 'admin'
Session::get('missing'); // null
Session::get('theme', 'light'); // 'light' (default)
Session::has('role'); // true
Session::all(); // ['username' => 'admin', 'role' => 'editor', ...]A stored null is returned as-is — the default only applies to missing keys:
Session::set('nickname', null);
Session::get('nickname', 'anon'); // null, not 'anon'Session::remove('csrf'); // delete a key (alias: delete())
$flash = Session::pull('flash'); // read, then remove — perfect for flash datasetAssoc() writes many string-keyed values at once. Pass true to replace the
whole session instead of merging:
Session::setAssoc([
'username' => 'admin',
'role' => 'editor',
]);
Session::setAssoc(['only' => 'this'], true); // wipes everything elseSession::isStarted(); // true
Session::regenerateId(true); // rotate the id (e.g. right after login)
Session::flush(); // clear all values (session stays open)
Session::destroy(); // tear the session down entirelyAfter createImmutable(), every method works both ways, against the same
session state:
$session = Session::createImmutable();
$session->start(); // instance call
Session::set('user', 'ada'); // static call — same session
echo $session->get('user'); // 'ada'The facade does not hide the superglobal:
$_SESSION['cart'] = ['sku-1', 'sku-2'];
echo count(Session::get('cart')); // 2- The Session Facade — how the pieces fit together.
- Reading & Writing Values — every value method.
- Session Lifecycle — start, regenerate, flush, destroy.
- Adapters — choose a storage backend.
initphp/sessions · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Core Usage
Adapters
- Adapters Overview
- File Adapter
- Redis Adapter
- PDO Adapter
- Cookie Adapter
- Memcache / Memcached Adapter
- MongoDB Adapter
- Custom Adapters
Reference
Practical Guides
Migration & Help