ez-php / session
Session handler drivers (File/Database/Redis/Array), StartSession middleware, flash data, ID regeneration
2.4.11
2026-09-20 01:19 UTC
Requires
- php: ^8.5
- ez-php/contracts: ^2.0
- ez-php/http: ^2.0
Requires (Dev)
- ez-php/docker: ^2.0
- friendsofphp/php-cs-fixer: ^3.94
- phpstan/phpstan: ^2.1
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^13.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Session handler drivers (File/Database/Redis/Array), StartSessionMiddleware, flash data, and session id regeneration for ez-php applications.
Installation
composer require ez-php/session
Usage
1. Register the service provider
$app->register(\EzPhp\Session\SessionServiceProvider::class);
2. Add StartSessionMiddleware before anything that reads the session
$app->middleware(\EzPhp\Session\StartSessionMiddleware::class);
Add it before ez-php/framework's CsrfMiddleware and before any ez-php/auth middleware — both read $_SESSION and assume a session is already active.
3. Configure the driver
config/session.php:
<?php return [ 'driver' => env('SESSION_DRIVER', 'file'), 'file' => [ 'path' => sys_get_temp_dir() . '/ez-session', ], 'database' => [ 'table' => 'sessions', ], 'redis' => [ 'host' => env('SESSION_REDIS_HOST', '127.0.0.1'), 'port' => (int) env('SESSION_REDIS_PORT', 6379), 'database' => (int) env('SESSION_REDIS_DATABASE', 0), 'ttl' => (int) env('SESSION_REDIS_TTL', 1440), ], // 0 disables periodic regeneration; StartSessionMiddleware otherwise // regenerates the session id once this many seconds have elapsed. 'regenerate_interval' => (int) env('SESSION_REGENERATE_INTERVAL', 0), ];
| Driver | Value | Notes |
|---|---|---|
| Array | array |
In-process only; for tests and CLI |
| File | file (default) |
Files under session.file.path |
| Database | database |
sessions table, created automatically |
| Redis | redis |
Native TTL, no gc() sweep needed |
4. Flash data
use EzPhp\Session\Flash; Flash::set('message', 'Saved successfully.'); // readable on the *next* request Flash::get('message'); // readable on *this* request, one round only Flash::keep('message'); // extend it for one more request
5. Session id regeneration
use EzPhp\Session\SessionRegenerator; SessionRegenerator::regenerate(); // unconditional SessionRegenerator::regenerateIfStale(1800); // only if 30 minutes have passed
ez-php/auth already regenerates the id on login/logout itself — SessionRegenerator is for periodic regeneration of a long-lived session, independent of authentication.
License
MIT