svn: /phpdoc/en/trunk/reference/session/functions/ session-set-save-handler.xml
| From: | Karma Dordrak | Date: | Sat, 25 Feb 2012 21:56:19 +0000 |
| Subject: | svn: /phpdoc/en/trunk/reference/session/functions/ session-set-save-handler.xml | ||
| Groups: | php.doc.cvs | ||
| Request: | Send a blank email to doc-cvs+get-9677@lists.php.net to get a copy of this message | ||
drak Sat, 25 Feb 2012 21:56:19 +0000
Revision: http://svn.php.net/viewvc?view=revision&revision=323543
Log:
[session-set-save-handler] Update examples and add notes on dealing with objects in the session
shutdown process.
Changed paths:
U phpdoc/en/trunk/reference/session/functions/session-set-save-handler.xml
Modified: phpdoc/en/trunk/reference/session/functions/session-set-save-handler.xml
===================================================================
--- phpdoc/en/trunk/reference/session/functions/session-set-save-handler.xml 2012-02-25 21:24:59 UTC
(rev 323542)
+++ phpdoc/en/trunk/reference/session/functions/session-set-save-handler.xml 2012-02-25 21:56:19 UTC
(rev 323543)
@@ -175,6 +175,15 @@
<title>
<function>session_set_save_handler</function> using
<classname>SessionHandlerInterface</classname>
</title>
+ <para>
+ The following code is for PHP version 5.4.0 and above.
+ </para>
+ <para>
+ The following example provides file based session storage similar to the
+ PHP sessions default save handler <parameter>files</parameter>. This
+ example could easily be extended to cover database storage using your
+ favorite PHP supported database engine.
+ </para>
<programlisting role="php">
<![CDATA[
<?php
@@ -185,6 +194,9 @@
public function open($savePath, $sessionName)
{
$this->savePath = $savePath;
+ if (!is_dir($this->savePath)) {
+ mkdir($this->savePath, 0777);
+ }
return true;
}
@@ -196,20 +208,12 @@
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 @@
}
$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
@@ -245,6 +249,9 @@
<example>
<title><function>session_set_save_handler</function> example</title>
<para>
+ The following code is for PHP versions less than 5.4.0.
+ </para>
+ <para>
The following example provides file based session storage similar to the
PHP sessions default save handler <parameter>files</parameter>. This
example could easily be extended to cover database storage using your
@@ -253,70 +260,70 @@
<programlisting role="php">
<![CDATA[
<?php
-function open($savePath, $sessionName)
+class FileSessionHandler
{
- global $sess_save_path;
+ private $savePath;
- $sess_save_path = $savePath;
+ function open($savePath, $sessionName)
+ {
+ $this->savePath = $savePath;
+ if (!is_dir($this->savePath)) {
+ mkdir($this->savePath, 0777);
+ }
- return true;
-}
+ return true;
+ }
-function close()
-{
- return true;
-}
+ function close()
+ {
+ return true;
+ }
-function read($id)
-{
- global $sess_save_path;
+ function read($id)
+ {
+ return (string)@file_get_contents("$this->savePath/sess_$id");
+ }
- $sess_file = "$sess_save_path/sess_$id";
+ function write($id, $data)
+ {
+ return file_put_contents("$savePath/sess_$id", $data) === false ? false : true;
+ }
- return (string)@file_get_contents($sess_file);
-}
+ function destroy($id)
+ {
+ $file = "$this->savePath/sess_$id";
+ if (file_exists($file)) {
+ unlink($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;
+ function gc($maxlifetime)
+ {
+ foreach (glob("$this->savePath/sess_*") as $file) {
+ if (filemtime($file) + $maxlifetime < time() && file_exists($file)) {
+ unlink($file);
+ }
+ }
- $file = "$sess_save_path/sess_$id";
- if (file_exists($file)) {
- unlink($file);
+ return true;
}
- return true;
}
-function gc($maxlifetime)
-{
- global $sess_save_path;
+$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')
+ );
- foreach (glob("$sess_save_path/sess_*") as $file) {
- if (filemtime($file) + $maxlifetime < time() && file_exists($file)) {
- unlink($file);
- }
- }
+// the following prevents unexpected effects when using objects as save handlers
+register_shutdown_function('session_write_close');
- return true;
-}
-
-session_set_save_handler("open", "close", "read", "write",
"destroy", "gc");
-
session_start();
// proceed to set and retrieve values by key from $_SESSION
]]>
@@ -329,6 +336,21 @@
&reftitle.notes;
<warning>
<para>
+ 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
+ <parameter>'session_write_close'</parameter> using the
+ <function>register_shutdown_function</function> function.
+ </para>
+ <para>
+ As of PHP 5.4.0 you can use <function>register_shutdown_function</function> or
+ simply use the 'register shutdown' flag when invoking
+ <function>session_set_save_handler</function> using the OOP method (passing an
+ instance that implements <classname>SessionHandlerInterface</classname>.
+ </para>
+ </warning>
+ <warning>
+ <para>
As of PHP 5.0.5 the <parameter>write</parameter> and
<parameter>close</parameter> handlers are called after object
destruction and therefore cannot use objects or throw exceptions.