Luminova Framework

PHP Luminova: Store PHP Sessions on the Filesystem

Last updated: 2026-11-28 20:57:12

Store session data in a writable directory with optional encryption and IP-based file selection.

Luminova's Session File Handler stores PHP sessions as files on the server. Register it with Session Handling before calling start(). Use a directory outside the public document root that the PHP process can read and write.

By default, the handler uses PHP's session.save_path when set, or the application's writeable/sessions/ directory. An explicit path is useful when the application needs its own session directory. If you also set App\Config\Session::$savePath, use the same directory for both settings.


Class Definition

  • Full namespace: Luminova\Sessions\Handlers\SessionFileHandler
  • This class extends Luminova\Base\AbstractSessionHandler.
  • This class inherits PHP's SessionHandlerInterface and SessionIdInterface from its parent.
  • This class is an ordinary class.

Save a preference across requests

Pass the same private directory to the handler on each request. After the user changes a preference, store it and close the session to write it. On a later request with the same session cookie, get() reads the saved value.

use Luminova\Sessions\Handlers\SessionFileHandler;
use Luminova\Sessions\Session;
use function Luminova\Funcs\root;

$directory = root('writeable/sessions');
$session = new Session();
$session->setHandler(new SessionFileHandler($directory));

if (!$session->start()) {
    throw new RuntimeException('Could not start the session.');
}

$theme = $session->get('theme', 'light');
$session->set('theme', 'dark');
$session->close();

Methods

__construct

Prepare the directory used for session files. Use an explicit path for application-specific storage and the shared handler options for encryption, session ID settings, and lifecycle callbacks.

public function __construct(?string $filePath = null, array $options = [])

Parameters:

ParameterTypeDescription
$filePathstring\|nullDirectory for the session files. Defaults to PHP's configured save path or the framework fallback (default: null).
$optionsarray<string,mixed>Shared handler options, including encryption and session_ip (default: []).

Throws:

Examples:

If application encryption is configured, enable encryption before registering the handler. With session_ip enabled, the client IP contributes to the filename, so a changed IP can select a different session file.

use Luminova\Sessions\Handlers\SessionFileHandler;
use function Luminova\Funcs\root;

$handler = new SessionFileHandler(root('writeable/sessions'), [
    'encryption' => true,
    'session_ip' => true,
    'sid_entropy_bits' => 160,
    'onCreate' => static function (string $path, string $name, string $file): bool {
        return is_writable($path);
    },
]);