joomla/session
Framework-agnostic PHP 8.1+ session management from the Joomla Framework. Provides interfaces and utilities to start and manage sessions, store and retrieve session data, and integrate session handling cleanly within your application.
Start by installing the package via Composer:
composer require joomla/session "~3.0"
The package requires PHP 8.1+, and provides a clean, object-oriented session API abstracting PHP’s native $_SESSION. Its core classes are Session, StorageInterface, and HandlerInterface. For basic use, inject NativeStorage into a Session instance and call start() explicitly when needed. Unlike legacy approaches, sessions are not auto-started—this gives you precise control over lifecycle and avoids side effects in CLI or testing contexts.
A minimal example:
use Joomla\Session\Session;
use Joomla\Session\Storage\NativeStorage;
$storage = new NativeStorage();
$session = new Session($input, $dispatcher, $storage);
$session->start();
$session->set('user.id', 42);
echo $session->get('user.id'); // 42
Key entry points are the SessionInterface and StorageInterface—design against these interfaces to remain flexible and testable.
getInstance() (removed in v2). Instead, register SessionInterface as a shared service in your DI container (e.g., in a Laravel service provider: app()->singleton(SessionInterface::class, fn() => new Session($input, $events, new NativeStorage()));).RuntimeStorage during unit tests or CLI commands to isolate sessions from real $_SESSION:
$storage = new RuntimeStorage();
$session = new Session(new ArrayInput(), $dispatcher, $storage);
getToken() and hasToken() for secure form handling:
<input type="hidden" name="{{ $session->getToken(true) }}" value="1">
Validate on submit:
if (!$session->hasToken($token)) {
abort(403, 'Invalid token');
}
onAfterSessionStart/onAfterSessionRestart events (via SessionEvent) to hydrate user context, audit logs, or token rotation.$session->fork() to migrate session data to a new ID, preventing fixation attacks.$session->start() before reading/writing is the most common mistake—expect silent failures or new session IDs on each request.$_SESSION['app']['key']). Store keys directly: set('user.id', 42) → writes to $_SESSION['user.id']. Adjust legacy code accordingly.ValidatorInterface instead of relying on joomla/input. Add validators via $session->addValidator($validator).HandlerInterface wraps PHP’s native session_set_save_handler() logic (e.g., DB, Redis), while StorageInterface handles $_SESSION interaction. A Redis-backed session needs both—a HandlerInterface implementation for persistence and NativeStorage (or custom) to bridge to Session.getToken($forceNew = true) forces regeneration every time—use only once per login/logout. For forms, generate once per request: getToken(false).$_SESSION, but sessions may be empty in CLI unless session.auto_start=1 or manually started. Prefer RuntimeStorage for CLI commands unless you deliberately need persistence._underscored methods, remove namespacing, and ensure Input/Event\Dispatcher are injected at construction. The upgrade guide in docs/v1-to-v2-update.md is critical.How can I help you explore Laravel packages today?