session_set_save_handler()

session_set_save_handler() — Sets user-level session storage functions.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

session_set_save_handler() sets the user-level session storage functions which are used for storing and retrieving data associated with a session. This is most useful when a storage method other than those supplied by PHP sessions is preferred, e.g. storing the session data in a local database.

Signature

session_set_save_handler(callable $open, callable $close, callable $read, callable $write, callable $destroy, callable $gc, callable $create_sid = ?, callable $validate_sid = ?, callable $update_timestamp = ?): bool session_set_save_handler(object $sessionhandler, bool $register_shutdown = true): bool open(string $savePath, string $sessionName): bool close(): bool read(string $sessionId): string|false write(string $sessionId, string $data): bool destroy(string $sessionId): bool gc(int $lifetime): bool create_sid(): string validate_sid(string $key): bool update_timestamp(string $key, string $val): bool

Parameters

ParameterTypeWhat it takes
$sessionhandlerobjectAn instance of a class implementing SessionHandlerInterface, and optionally SessionIdInterface and/or SessionUpdateTimestampHandlerInterface, such as SessionHandler, to register as the session handler.
$register_shutdownboolRegister session_write_close() as a register_shutdown_function() function.

Return value

Returns true on success or false on failure.

Example

A runnable snippet:

class MySessionHandler implements SessionHandlerInterface { // implement interfaces here } $handler = new MySessionHandler(); session_set_save_handler($handler, true); session_start(); // proceed to set and retrieve values by key from $_SESSION

Changelog

VersionChange
8.4.0Calling session_set_save_handler() with more than two arguments is now deprecated. Use the two argument signature instead.

See also

register_shutdown_function(), session_register_shutdown()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page session_set_save_handler() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.