Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 5731

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Sets user-level session storage functions</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.session-set-cookie-params.html">session_set_cookie_params</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.session-start.html">session_start</a></div>
 <div class="up"><a href="ref.session.html">Session Functions</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.session-set-save-handler" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">session_set_save_handler</h1>
  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">session_set_save_handler</span> &mdash; <span class="dc-title">Sets user-level session storage functions</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.session-set-save-handler-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">bool</span> <span class="methodname"><strong>session_set_save_handler</strong></span>
    ( <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$open</code></span>
   , <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$close</code></span>
   , <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$read</code></span>
   , <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$write</code></span>
   , <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$destroy</code></span>
   , <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$gc</code></span>
   )</div>

  <p class="para rdfs-comment">
   Since PHP 5.4 it is possible to register the following prototype:
  </p>
  <div class="methodsynopsis dc-description">
   <span class="type">bool</span> <span class="methodname"><strong>session_set_save_handler</strong></span>
    ( <span class="methodparam"><span class="type"><a href="class.sessionhandlerinterface.html" class="type SessionHandlerInterface">SessionHandlerInterface</a></span> <code class="parameter">$sessionhandler</code></span>
   [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$register_shutdown</code><span class="initializer"> = true</span></span>
  ] )</div>

  <p class="para rdfs-comment">
    <span class="function"><strong>session_set_save_handler()</strong></span> 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.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.session-set-save-handler-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   This function has two prototypes.
   <dl>

    <dt>

     <span class="term"><em><code class="parameter">sessionhandler</code></em></span>
     <dd>

      <p class="para">
       An instance of a class implementing
       <span class="interfacename"><a href="class.sessionhandlerinterface.html" class="interfacename">SessionHandlerInterface</a></span>, such as
       <a href="class.sessionhandler.html" class="classname">SessionHandler</a>, to register as the session
       handler. Since PHP 5.4 only.
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">register_shutdown</code></em></span>
     <dd>

      <p class="para">
       Register  <span class="function"><a href="function.session-write-close.html" class="function">session_write_close()</a></span> as a
        <span class="function"><a href="function.register-shutdown-function.html" class="function">register_shutdown_function()</a></span> function.
      </p>
     </dd>

    </dt>

   </dl>


   or

   <dl>

    <dt>

     <span class="term"><em><code class="parameter">open(string $savePath, string $sessionName)</code></em></span>
     <dd>

      <p class="para">
       The open callback works like a constructor in classes and is
       executed when the session is being opened.  It is the first callback
       function executed when the session is started automatically or
       manually with  <span class="function"><a href="function.session-start.html" class="function">session_start()</a></span>.
       Return value is <strong><code>TRUE</code></strong> for success, <strong><code>FALSE</code></strong> for failure.
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">close()</code></em></span>
     <dd>

      <p class="para">
       The close callback works like a destructor in classes and is
       executed after the session write callback has been called. It is also invoked when
        <span class="function"><a href="function.session-write-close.html" class="function">session_write_close()</a></span> is called.
       Return value should be <strong><code>TRUE</code></strong> for success, <strong><code>FALSE</code></strong> for failure.
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">read(string $sessionId)</code></em></span>
     <dd>

      <p class="para">
       The <em><code class="parameter">read</code></em> callback must always return a session encoded (serialized)
       string, or an empty string if there is no data to read.
      </p>
      <p class="para">
       This callback is called internally by PHP when the session starts or
       when  <span class="function"><a href="function.session-start.html" class="function">session_start()</a></span> is called.  Before this callback is invoked
       PHP will invoke the <em><code class="parameter">open</code></em> callback.
      </p>
      <p class="para">
       The value this callback returns must be in exactly the same serialized format that was originally
       passed for storage to the <em><code class="parameter">write</code></em> callback.  The value returned will be
       unserialized automatically by PHP and used to populate the <var class="varname"><var class="varname"><a href="reserved.variables.session.html" class="classname">$_SESSION</a></var></var> superglobal.
       While the data looks similar to  <span class="function"><a href="function.serialize.html" class="function">serialize()</a></span> please note it is a different format
       which is speficied in the <a href="session.configuration.html#ini.session.serialize-handler" class="link">session.serialize_handler</a> ini setting.
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">write(string $sessionId, string $data)</code></em></span>
     <dd>

      <p class="para">
       The <em><code class="parameter">write</code></em> callback is called when the session needs to be saved and closed. This
       callback receives the current session ID a serialized version the <var class="varname"><var class="varname"><a href="reserved.variables.session.html" class="classname">$_SESSION</a></var></var> superglobal.  The serialization
       method used internally by PHP is specified in the <a href="session.configuration.html#ini.session.serialize-handler" class="link">session.serialize_handler</a> ini setting.
      </p>
      <p class="para">
       The serialized session data passed to this callback should be stored against the passed session ID. When retrieving
       this data, the <em><code class="parameter">read</code></em> callback must return the exact value that was originally passed to
       the <em><code class="parameter">write</code></em> callback.
      </p>
      <p class="para">
       This callback is invoked when PHP shuts down or explicitly when  <span class="function"><a href="function.session-write-close.html" class="function">session_write_close()</a></span>
       is called.  Note that after executing this function PHP will internally execute the <em><code class="parameter">close</code></em> callback.
       <blockquote class="note"><p><strong class="note">Note</strong>: 
        <p class="para">
         The &quot;write&quot; handler is not executed until after the output stream is
         closed.  Thus, output from debugging statements in the &quot;write&quot;
         handler will never be seen in the browser.  If debugging output is
         necessary, it is suggested that the debug output be written to a
         file instead.
        </p>
       </p></blockquote>
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">destroy($sessionId)</code></em></span>
     <dd>

      <p class="para">
       This callback is executed when a session is destroyed with  <span class="function"><a href="function.session-destroy.html" class="function">session_destroy()</a></span> or with
        <span class="function"><a href="function.session-regenerate-id.html" class="function">session_regenerate_id()</a></span> with the destroy parameter set to <strong><code>TRUE</code></strong>.
       Return value should be <strong><code>TRUE</code></strong> for success, <strong><code>FALSE</code></strong> for failure.
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">gc($lifetime)</code></em></span>
     <dd>

      <p class="para">
       The garbage collector callback is invoked internally by PHP periodically in order to
       purge old session data.  The frequency is controlled by
       <a href="session.configuration.html#ini.session.gc-probability" class="link">session.gc_probability</a> and <a href="session.configuration.html#ini.session.gc-divisor" class="link">session.gc_divisor</a>.
       The value of lifetime which is passed to this callback can be set in <a href="session.configuration.html#ini.session.gc-maxlifetime" class="link">session.gc_maxlifetime</a>.
       Return value should be <strong><code>TRUE</code></strong> for success, <strong><code>FALSE</code></strong> for failure.
      </p>
     </dd>

    </dt>
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.session-set-save-handler-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns <strong><code>TRUE</code></strong> on success or <strong><code>FALSE</code></strong> on failure.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.session-set-save-handler-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-4770">
    <p><strong>Example #1 
     Custom session handler: see full code in <a href="class.sessionhandlerinterface.html" class="classname">SessionHandlerInterface</a> synposis.
    </strong></p>
    <div class="example-contents"><p>
     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 <a href="class.sessionhandlerinterface.html" class="classname">SessionHandlerInterface</a> synposis linked above.
    </p></div>
    <div class="example-contents"><p>
     Note we use the OOP prototype with  <span class="function"><strong>session_set_save_handler()</strong></span> and
     register the shutdown function using the function&#039;s parameter flag.  This is generally
     advised when registering objects as session save handlers.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">MySessionHandler&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">SessionHandlerInterface<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;implement&nbsp;interfaces&nbsp;here<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #0000BB">$handler&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MySessionHandler</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">session_set_save_handler</span><span style="color: #007700">(</span><span style="color: #0000BB">$handler</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">session_start</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">//&nbsp;proceed&nbsp;to&nbsp;set&nbsp;and&nbsp;retrieve&nbsp;values&nbsp;by&nbsp;key&nbsp;from&nbsp;$_SESSION</span>
</span>
</code></div>
    </div>

   </div>
   <div class="example" id="example-4771">
    <p><strong>Example #2 Custom session save handler using objects</strong></p>
    <div class="example-contents"><p>
     The following code is for PHP versions less than 5.4.0.
    </p></div>
    <div class="example-contents"><p>
     The following example provides file based session storage similar to the
     PHP sessions default save handler <em><code class="parameter">files</code></em>.  This
     example could easily be extended to cover database storage using your
     favorite PHP supported database engine.
    </p></div>
    <div class="example-contents"><p>
     Note we additionally register the shutdown function  <span class="function"><a href="function.session-write-close.html" class="function">session_write_close()</a></span>
     using  <span class="function"><a href="function.register-shutdown-function.html" class="function">register_shutdown_function()</a></span> 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.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">FileSessionHandler<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">$savePath</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">open</span><span style="color: #007700">(</span><span style="color: #0000BB">$savePath</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$sessionName</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">savePath&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$savePath</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!</span><span style="color: #0000BB">is_dir</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">savePath</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">mkdir</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">savePath</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0777</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">close</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">read</span><span style="color: #007700">(</span><span style="color: #0000BB">$id</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;(string)@</span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">savePath</span><span style="color: #DD0000">/sess_</span><span style="color: #0000BB">$id</span><span style="color: #DD0000">"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">write</span><span style="color: #007700">(</span><span style="color: #0000BB">$id</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">file_put_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">savePath</span><span style="color: #DD0000">/sess_</span><span style="color: #0000BB">$id</span><span style="color: #DD0000">"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">)&nbsp;===&nbsp;</span><span style="color: #0000BB">false&nbsp;</span><span style="color: #007700">?&nbsp;</span><span style="color: #0000BB">false&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">destroy</span><span style="color: #007700">(</span><span style="color: #0000BB">$id</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$file&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">savePath</span><span style="color: #DD0000">/sess_</span><span style="color: #0000BB">$id</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">file_exists</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">unlink</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">gc</span><span style="color: #007700">(</span><span style="color: #0000BB">$maxlifetime</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">glob</span><span style="color: #007700">(</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">savePath</span><span style="color: #DD0000">/sess_*"</span><span style="color: #007700">)&nbsp;as&nbsp;</span><span style="color: #0000BB">$file</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">filemtime</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">)&nbsp;+&nbsp;</span><span style="color: #0000BB">$maxlifetime&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">time</span><span style="color: #007700">()&nbsp;&amp;&amp;&nbsp;</span><span style="color: #0000BB">file_exists</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">unlink</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$handler&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">FileSessionHandler</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">session_set_save_handler</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #0000BB">$handler</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'open'</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #0000BB">$handler</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'close'</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #0000BB">$handler</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'read'</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #0000BB">$handler</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'write'</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #0000BB">$handler</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'destroy'</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #0000BB">$handler</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'gc'</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;);<br /><br /></span><span style="color: #FF8000">//&nbsp;the&nbsp;following&nbsp;prevents&nbsp;unexpected&nbsp;effects&nbsp;when&nbsp;using&nbsp;objects&nbsp;as&nbsp;save&nbsp;handlers<br /></span><span style="color: #0000BB">register_shutdown_function</span><span style="color: #007700">(</span><span style="color: #DD0000">'session_write_close'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">session_start</span><span style="color: #007700">();<br /></span><span style="color: #FF8000">//&nbsp;proceed&nbsp;to&nbsp;set&nbsp;and&nbsp;retrieve&nbsp;values&nbsp;by&nbsp;key&nbsp;from&nbsp;$_SESSION</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.session-set-save-handler-notes">
  <h3 class="title">Notes</h3>
  <div class="warning"><strong class="warning">Warning</strong>
   <p class="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 and may prevent the
    <em><code class="parameter">write</code></em> and <em><code class="parameter">close</code></em> from being called.
    Typically you should register <em><code class="parameter">&#039;session_write_close&#039;</code></em> using the
     <span class="function"><a href="function.register-shutdown-function.html" class="function">register_shutdown_function()</a></span> function.
   </p>
   <p class="para">
    As of PHP 5.4.0 you can use  <span class="function"><a href="function.session-register-shutdown.html" class="function">session_register_shutdown()</a></span> or
    simply use the &#039;register shutdown&#039; flag when invoking
     <span class="function"><strong>session_set_save_handler()</strong></span> using the OOP method and passing an
    instance that implements <a href="class.sessionhandlerinterface.html" class="classname">SessionHandlerInterface</a>.
   </p>
  </div>
  <div class="warning"><strong class="warning">Warning</strong>
   <p class="para">
    As of PHP 5.0.5 the <em><code class="parameter">write</code></em> and
    <em><code class="parameter">close</code></em> handlers are called after object
    destruction and therefore cannot use objects or throw exceptions.
    Exceptions are not able to be caught since will not be caught nor will
    any exception trace be displayed and the execution will just cease unexpectedly.
    The object destructors can however use sessions.
   </p>
   <p class="para">
    It is possible to call  <span class="function"><a href="function.session-write-close.html" class="function">session_write_close()</a></span> from the
    destructor to solve this chicken and egg problem but the most reliable way is
    to register the shutdown function as described above.
   </p>
  </div>
  <div class="warning"><strong class="warning">Warning</strong>
   <p class="para">
    Current working directory is changed with some SAPIs if session is
    closed in the script termination. It is possible to close the session
    earlier with  <span class="function"><a href="function.session-write-close.html" class="function">session_write_close()</a></span>.
   </p>
  </div>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.session-set-save-handler-changelog">
  <h3 class="title">Changelog</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Version</th>
      <th>Description</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>5.4.0</td>
      <td>
       Added <span class="interfacename"><a href="class.sessionhandlerinterface.html" class="interfacename">SessionHandlerInterface</a></span> for implementing session handlers and
       <a href="class.sessionhandler.html" class="classname">SessionHandler</a> to expose internal PHP session handlers.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 seealso" id="refsect1-function.session-set-save-handler-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member">
     The <a href="session.configuration.html#ini.session.save-handler" class="link">session.save_handler</a>
     configuration directive
    </li>
    <li class="member">
     The <a href="session.configuration.html#ini.session.serialize-handler" class="link">session.serialize_handler</a>
     configuration directive.
    </li>
    <li class="member">The  <span class="function"><a href="function.register-shutdown-function.html" class="function" rel="rdfs-seeAlso">register_shutdown_function()</a> - Register a function for execution on shutdown</span></li>
    <li class="member">The  <span class="function"><a href="function.session-register-shutdown.html" class="function" rel="rdfs-seeAlso">session_register_shutdown()</a> - Session shutdown function</span> for PHP 5.4.0+</li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.session-set-cookie-params.html">session_set_cookie_params</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.session-start.html">session_start</a></div>
 <div class="up"><a href="ref.session.html">Session Functions</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>