diff --git a/reference/session/functions/session-set-save-handler.xml b/reference/session/functions/session-set-save-handler.xml
index ce02f43994..7a24369525 100644
--- a/reference/session/functions/session-set-save-handler.xml
+++ b/reference/session/functions/session-set-save-handler.xml
@@ -175,6 +175,15 @@
session_set_save_handler using SessionHandlerInterface
+
+ The following code is for PHP version 5.4.0 and above.
+
+
+ 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.
+
savePath = $savePath;
+ if (!is_dir($this->savePath)) {
+ mkdir($this->savePath, 0777);
+ }
return true;
}
@@ -196,20 +208,12 @@ class MySessionHandler implements SessionHandlerInterface
public function read($id)
{
- $file = "$this->savePath/sess_$id";
- return (string)@file_get_contents($file);
+ return (string)@file_get_contents("$this->savePath/sess_$id");
}
public function write($id, $data)
{
- $file = "$this->savePath/sess_$id";
- if ($fp = @fopen($file, "w")) {
- $return = fwrite($fp, $data);
- fclose($fp);
- return $return;
- } else {
- return false;
- }
+ return file_put_contents("$savePath/sess_$id", $data) === false ? false : true;
}
public function destroy($id)
@@ -235,7 +239,7 @@ class MySessionHandler implements SessionHandlerInterface
}
$handler = new MySessionHandler();
-session_set_save_handler($handler);
+session_set_save_handler($handler, true);
session_start();
// proceed to set and retrieve values by key from $_SESSION
@@ -244,6 +248,9 @@ session_start();
session_set_save_handler example
+
+ 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
@@ -253,69 +260,69 @@ session_start();
savePath = $savePath;
+ if (!is_dir($this->savePath)) {
+ mkdir($this->savePath, 0777);
+ }
- return true;
-}
-
-function close()
-{
- return true;
-}
-
-function read($id)
-{
- global $sess_save_path;
-
- $sess_file = "$sess_save_path/sess_$id";
-
- return (string)@file_get_contents($sess_file);
-}
-
-function write($id, $data)
-{
- global $sess_save_path;
-
- $sess_file = "$sess_save_path/sess_$id";
- if ($fp = @fopen($sess_file, "w")) {
- $return = fwrite($fp, $data);
- fclose($fp);
-
- return $return;
- } else {
- return false;
+ return true;
}
-}
-function destroy($id)
-{
- global $sess_save_path;
-
- $file = "$sess_save_path/sess_$id";
- if (file_exists($file)) {
- unlink($file);
+ function close()
+ {
+ return true;
}
- return true;
-}
-function gc($maxlifetime)
-{
- global $sess_save_path;
+ function read($id)
+ {
+ return (string)@file_get_contents("$this->savePath/sess_$id");
+ }
- foreach (glob("$sess_save_path/sess_*") as $file) {
- if (filemtime($file) + $maxlifetime < time() && file_exists($file)) {
+ function write($id, $data)
+ {
+ return file_put_contents("$savePath/sess_$id", $data) === false ? false : true;
+ }
+
+ function destroy($id)
+ {
+ $file = "$this->savePath/sess_$id";
+ if (file_exists($file)) {
unlink($file);
}
+
+ return true;
}
- return true;
+ function gc($maxlifetime)
+ {
+ foreach (glob("$this->savePath/sess_*") as $file) {
+ if (filemtime($file) + $maxlifetime < time() && file_exists($file)) {
+ unlink($file);
+ }
+ }
+
+ return true;
+ }
}
-session_set_save_handler("open", "close", "read", "write", "destroy", "gc");
+$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
@@ -327,6 +334,21 @@ session_start();
&reftitle.notes;
+
+
+ When using objects as session save handlers, it is important to register the
+ shutdown function with PHP to avoid unexpected side-effects from the way
+ PHP internally destroys objects on shutdown. Typically you should register
+ 'session_write_close' using the
+ register_shutdown_function function.
+
+
+ As of PHP 5.4.0 you can use register_shutdown_function or
+ simply use the 'register shutdown' flag when invoking
+ session_set_save_handler using the OOP method (passing an
+ instance that implements SessionHandlerInterface.
+
+
As of PHP 5.0.5 the write and