-
Notifications
You must be signed in to change notification settings - Fork 0
File Adapter
InitPHP\Sessions\Adapters\FileAdapter stores each session as a file in a
directory you choose — without touching the global session.save_path ini
setting.
Requirements: none.
| Option | Type | Default | Notes |
|---|---|---|---|
path |
string | — | Required. An existing, writable directory. |
prefix |
string | sess_ |
Prefix for each session file name. |
If path is missing, or is not a writable directory, the constructor throws a
SessionInvalidArgumentException.
use InitPHP\Sessions\Session;
use InitPHP\Sessions\Adapters\FileAdapter;
$adapter = new FileAdapter([
'path' => '/var/www/storage/sessions',
'prefix' => 'sess_',
]);
Session::createImmutable($adapter)->start();A session with id abc123 is stored at /var/www/storage/sessions/sess_abc123.
gc() runs on PHP's normal session GC schedule (governed by
session.gc_probability / session.gc_divisor / session.gc_maxlifetime). It
deletes every file in path whose modification time is older than
gc_maxlifetime, and returns the number of files removed.
You can also trigger it manually:
$deleted = $adapter->gc(3600); // delete sessions idle for > 1 hour-
read()returns the file's contents, or''if the file does not exist (a brand-new session). -
write()overwrites the file atomically viafile_put_contents(). -
destroy()deletes the file if present and always reports success (destroying a non-existent session is not an error).
- You want a non-default save path or file prefix without editing
php.ini. - Single-server deployments, or shared storage (NFS/EFS) across a small fleet.
For multi-server setups where shared storage is awkward, prefer Redis or PDO.
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