Since PHP 5.4 it is possible to register the following prototype:
$sessionhandler
[, bool $register_shutdown
= true ] )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. i.e. Storing the session data in a local database.
An instance of a class implementing SessionHandlerInterface, such as SessionHandler, to register as the session handler. Since PHP 5.4 only.
Register session_write_close() as a register_shutdown_function() function.
Returns TRUE
on success or FALSE
on failure.
Added the optional create_sid
parameter.
Added SessionHandlerInterface for implementing session handlers and SessionHandler to expose internal PHP session handlers.
The following code is for PHP version 5.4.0 and above. We just show the invokation here, the full example can be seen in the SessionHandlerInterface synposis linked above.
Note we use the OOP prototype with session_set_save_handler() and register the shutdown function using the function's parameter flag. This is generally advised when registering objects as session save handlers.
<?php 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
The following code is for PHP versions less than 5.4.0.
The following example provides file based session storage similar to the PHP sessions default save handler files
. This example could easily be extended to cover database storage using your favorite PHP supported database engine.
Note we additionally register the shutdown function session_write_close() using register_shutdown_function() under PHP less than 5.4.0. This is generally advised when registering objects as session save handlers under PHP less than 5.4.0.
<?php class FileSessionHandler { private $savePath; function open($savePath, $sessionName) { $this->savePath = $savePath; if (!is_dir($this->savePath)) { mkdir($this->savePath, 0777); } return true; } function close() { return true; } function read($id) { return (string)@file_get_contents("$this->savePath/sess_$id"); } function write($id, $data) { return file_put_contents("$this->savePath/sess_$id", $data) === false ? false : true; } function destroy($id) { $file = "$this->savePath/sess_$id"; if (file_exists($file)) { unlink($file); } return true; } function gc($maxlifetime) { foreach (glob("$this->savePath/sess_*") as $file) { if (filemtime($file) + $maxlifetime < time() && file_exists($file)) { unlink($file); } } return true; } } $handler = new FileSessionHandler(); session_set_save_handler( array($handler, 'open'), array($handler, 'close'), array($handler, 'read'), array($handler, 'write'), array($handler, 'destroy'), array($handler, 'gc') ); // the following prevents unexpected effects when using objects as save handlers register_shutdown_function('session_write_close'); session_start(); // proceed to set and retrieve values by key from $_SESSION
Please login to continue.