svn: /phpdoc/en/trunk/reference/session/ functions/session-set-save-handler.xml sessionhandlerinterface.xml

From: Date: Tue, 28 Feb 2012 14:03:19 +0000
Subject: svn: /phpdoc/en/trunk/reference/session/ functions/session-set-save-handler.xml sessionhandlerinterface.xml
Groups: php.doc.cvs 
Request: Send a blank email to doc-cvs+get-9701@lists.php.net to get a copy of this message
drak Tue, 28 Feb 2012 14:03:19 +0000 Revision: http://svn.php.net/viewvc?view=revision&revision=323629 Log: [sessions] move one example code from session-set-save-handler to sessionhandlerinterface Changed paths: U phpdoc/en/trunk/reference/session/functions/session-set-save-handler.xml U phpdoc/en/trunk/reference/session/sessionhandlerinterface.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-28 13:37:00 UTC (rev 323628) +++ phpdoc/en/trunk/reference/session/functions/session-set-save-handler.xml 2012-02-28 14:03:19 UTC (rev 323629) @@ -173,18 +173,13 @@ <para> <example> <title> - <function>session_set_save_handler</function> using <classname>SessionHandlerInterface</classname> + Custom session handler: see full code in <classname>SessionHandlerInterface</classname> synposis. </title> <para> - The following code is for PHP version 5.4.0 and above. + 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 <classname>SessionHandlerInterface</classname> synposis linked 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> - <para> Note we use the OOP prototype with <function>session_set_save_handler</function> and register the shutdown function using the function's parameter flag. This is generally advised when registering objects as session save handlers. @@ -194,53 +189,7 @@ <?php class MySessionHandler implements SessionHandlerInterface { - private $savePath; - - public function open($savePath, $sessionName) - { - $this->savePath = $savePath; - if (!is_dir($this->savePath)) { - mkdir($this->savePath, 0777); - } - - return true; - } - - public function close() - { - return true; - } - - public function read($id) - { - return (string)@file_get_contents("$this->savePath/sess_$id"); - } - - public function write($id, $data) - { - return file_put_contents("$savePath/sess_$id", $data) === false ? false : true; - } - - public function destroy($id) - { - $file = "$this->savePath/sess_$id"; - if (file_exists($file)) { - unlink($file); - } - - return true; - } - - public function gc($maxlifetime) - { - foreach (glob("$this->savePath/sess_*") as $file) { - if (filemtime($file) + $maxlifetime < time() && file_exists($file)) { - unlink($file); - } - } - - return true; - } + // implement interfaces here } $handler = new MySessionHandler(); @@ -399,9 +348,8 @@ <row> <entry>5.4.0</entry> <entry> - Added <interfacename>SessionHandlerInterface</interfacename> and - <classname>SessionHandler</classname> for implementing session - handlers. + Added <interfacename>SessionHandlerInterface</interfacename> for implementing session handlers and + <classname>SessionHandler</classname> to expose internal PHP session handlers. </entry> </row> </tbody> @@ -417,6 +365,10 @@ The <link linkend="ini.session.save-handler">session.save_handler</link> configuration directive </member> + <member> + The <link linkend="ini.session.serialize-handler">session.serialize_handler</link> + configuration directive. + </member> <member>The <function>register_shutdown_function</function></member> <member>The <function>session_register_shutdown</function> for PHP 5.4.0+</member> </simplelist> Modified: phpdoc/en/trunk/reference/session/sessionhandlerinterface.xml =================================================================== --- phpdoc/en/trunk/reference/session/sessionhandlerinterface.xml 2012-02-28 13:37:00 UTC (rev 323628) +++ phpdoc/en/trunk/reference/session/sessionhandlerinterface.xml 2012-02-28 14:03:19 UTC (rev 323629) @@ -47,6 +47,86 @@ </section> + <section xml:id="sessionhandlerinterface.examples"> + <example> + <title> + Example using <classname>SessionHandlerInterface</classname> + </title> + <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> + <para> + Note we use the OOP prototype with <function>session_set_save_handler</function> and + register the shutdown function using the function's parameter flag. This is generally + advised when registering objects as session save handlers. + </para> + <programlisting role="php"> +<![CDATA[ +<?php +class MySessionHandler implements SessionHandlerInterface +{ + private $savePath; + + public function open($savePath, $sessionName) + { + $this->savePath = $savePath; + if (!is_dir($this->savePath)) { + mkdir($this->savePath, 0777); + } + + return true; + } + + public function close() + { + return true; + } + + public function read($id) + { + return (string)@file_get_contents("$this->savePath/sess_$id"); + } + + public function write($id, $data) + { + return file_put_contents("$savePath/sess_$id", $data) === false ? false : true; + } + + public function destroy($id) + { + $file = "$this->savePath/sess_$id"; + if (file_exists($file)) { + unlink($file); + } + + return true; + } + + public function gc($maxlifetime) + { + foreach (glob("$this->savePath/sess_*") as $file) { + if (filemtime($file) + $maxlifetime < time() && file_exists($file)) { + unlink($file); + } + } + + return true; + } +} + +$handler = new MySessionHandler(); +session_set_save_handler($handler, true); +session_start(); + +// proceed to set and retrieve values by key from $_SESSION +]]> + </programlisting> + </example> + </section> + </partintro> &reference.session.entities.sessionhandlerinterface;

« previous php.doc.cvs (#9701) next »