Skip to content

Storage handlers

Greg Bowler edited this page Apr 23, 2026 · 1 revision

PHP separates session behaviour from session storage through SessionHandlerInterface.

This library can work with any object that implements that interface. It also provides two handlers:

  • GT\Session\FileHandler
  • GT\Session\RedisHandler

FileHandler

FileHandler stores each session as a file.

use GT\Session\FileHandler;
use GT\Session\Session;

$session = new Session(new FileHandler(), [
	"name" => "GT",
	"save_path" => "phpgt/session",
]);

The handler stores files under:

<save-path>/<session-name>/<session-id>

When the save path is relative, Session places it under sys_get_temp_dir() before the handler receives it.

Garbage collection

FileHandler::gc() deletes session files whose modification time is older than the maximum lifetime passed by PHP.

The method returns the number of deleted files, or false if a file could not be deleted.

Empty sessions

FileHandler does not write PHP's empty serialised array value, a:0:{}. That avoids creating files for sessions that have no useful data.

Custom handlers

Any custom handler can be used as long as it implements SessionHandlerInterface.

use GT\Session\Session;

$handler = new MySessionHandler();
$session = new Session($handler, [
	"name" => "GT",
	"save_path" => "custom/path",
]);

If the handler should be registered with PHP's session system, use SessionSetup.

use GT\Session\SessionSetup;

$setup = new SessionSetup();
$handler = $setup->attachHandler(MySessionHandler::class);

Choosing a handler

Use file storage when the application runs on one server, or when the filesystem is shared safely between all application nodes.

Use Redis-compatible storage when application nodes are disposable, load-balanced, or do not share a filesystem.

Note

WebEngine reads the handler class from [session] handler in config.ini. Older projects may still show Gt\Session\FileHandler; Composer keeps that prefix working, but new configuration can use GT\Session\FileHandler.


Next, configure shared storage in Redis session storage.

Clone this wiki locally