<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.session.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'uk',
  ),
  'this' => 
  array (
    0 => 'function.session-set-save-handler.php',
    1 => 'session_set_save_handler',
    2 => 'Sets user-level session storage functions',
  ),
  'up' => 
  array (
    0 => 'ref.session.php',
    1 => 'Session Функції',
  ),
  'prev' => 
  array (
    0 => 'function.session-set-cookie-params.php',
    1 => 'session_set_cookie_params',
  ),
  'next' => 
  array (
    0 => 'function.session-start.php',
    1 => 'session_start',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/session/functions/session-set-save-handler.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<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, PHP 7, PHP 8)</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">Опис</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>session_set_save_handler</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span> <code class="parameter">$open</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span> <code class="parameter">$close</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span> <code class="parameter">$read</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span> <code class="parameter">$write</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span> <code class="parameter">$destroy</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span> <code class="parameter">$gc</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span> <code class="parameter">$create_sid</code><span class="initializer"> = ?</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span> <code class="parameter">$validate_sid</code><span class="initializer"> = ?</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span> <code class="parameter">$update_timestamp</code><span class="initializer"> = ?</span></span><br>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

  <p class="para rdfs-comment">
   It is possible to register the following prototype:
  </p>
  <div class="methodsynopsis dc-description"><span class="methodname"><strong>session_set_save_handler</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.object.php" class="type object">object</a></span> <code class="parameter">$sessionhandler</code></span>, <span class="methodparam"><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> <code class="parameter">$register_shutdown</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong></span></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></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, e.g. 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">Параметри</h3>
  <p class="para">
   This function has two prototypes.
   <dl>
    
     <dt><code class="parameter">sessionhandler</code></dt>
     <dd>
      <p class="para">
       An instance of a class implementing
       <span class="interfacename"><a href="class.sessionhandlerinterface.php" class="interfacename">SessionHandlerInterface</a></span>, and optionally
       <span class="interfacename"><a href="class.sessionidinterface.php" class="interfacename">SessionIdInterface</a></span> and/or
       <span class="interfacename"><a href="class.sessionupdatetimestamphandlerinterface.php" class="interfacename">SessionUpdateTimestampHandlerInterface</a></span>, such as
       <span class="classname"><a href="class.sessionhandler.php" class="classname">SessionHandler</a></span>, to register as the session
       handler.
      </p>
     </dd>
    
    
     <dt><code class="parameter">register_shutdown</code></dt>
     <dd>
      <p class="para">
       Register <span class="function"><a href="function.session-write-close.php" class="function">session_write_close()</a></span> as a
       <span class="function"><a href="function.register-shutdown-function.php" class="function">register_shutdown_function()</a></span> function.
      </p>
     </dd>
    
   </dl>

   or

   <dl>
    
     <dt><code class="parameter">open</code></dt>
     <dd>
      <p class="para">
       A callable with the following signature:
       <div class="methodsynopsis dc-description"><span class="methodname"><span class="replaceable">open</span></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$savePath</code></span>, <span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$sessionName</code></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

      </p>
      <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.php" class="function">session_start()</a></span>.
       Return value is <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> for success, <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> for failure.
      </p>
     </dd>
    
    
     <dt><code class="parameter">close</code></dt>
     <dd>
      <p class="para">
       A callable with the following signature:
       <div class="methodsynopsis dc-description"><span class="methodname"><span class="replaceable">close</span></span>(): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

      </p>
      <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.php" class="function">session_write_close()</a></span> is called.
       Return value should be <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> for success, <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> for failure.
      </p>
     </dd>
    
    
     <dt><code class="parameter">read</code></dt>
     <dd>
      <p class="para">
       A callable with the following signature:
       <div class="methodsynopsis dc-description"><span class="methodname"><span class="replaceable">read</span></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$sessionId</code></span>): <span class="type"><a href="language.types.string.php" class="type string">string</a></span></div>

      </p>
      <p class="para">
       The <code class="parameter">read</code> 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.php" class="function">session_start()</a></span> is called.  Before this callback is invoked
       PHP will invoke the <code class="parameter">open</code> 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 <code class="parameter">write</code> callback.  The value returned will be
       unserialized automatically by PHP and used to populate the <var class="varname"><a href="reserved.variables.session.php" class="classname">$_SESSION</a></var> superglobal.
       While the data looks similar to <span class="function"><a href="function.serialize.php" class="function">serialize()</a></span> please note it is a different format
       which is specified in the <a href="session.configuration.php#ini.session.serialize-handler" class="link">session.serialize_handler</a> ini setting.
      </p>
     </dd>
    
    
     <dt><code class="parameter">write</code></dt>
     <dd>
      <p class="para">
       A callable with the following signature:
       <div class="methodsynopsis dc-description"><span class="methodname"><span class="replaceable">write</span></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$sessionId</code></span>, <span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$data</code></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

      </p>
      <p class="para">
       The <code class="parameter">write</code> 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"><a href="reserved.variables.session.php" class="classname">$_SESSION</a></var> superglobal.  The serialization
       method used internally by PHP is specified in the <a href="session.configuration.php#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 <code class="parameter">read</code> callback must return the exact value that was originally passed to
       the <code class="parameter">write</code> 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.php" class="function">session_write_close()</a></span>
       is called.  Note that after executing this function PHP will internally execute the <code class="parameter">close</code> callback.
       <blockquote class="note"><p><strong class="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><code class="parameter">destroy</code></dt>
     <dd>
      <p class="para">
       A callable with the following signature:
       <div class="methodsynopsis dc-description">
        <span class="methodname"><span class="replaceable">destroy</span></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$sessionId</code></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

      </p>
      <p class="para">
       This callback is executed when a session is destroyed with <span class="function"><a href="function.session-destroy.php" class="function">session_destroy()</a></span> or with
       <span class="function"><a href="function.session-regenerate-id.php" class="function">session_regenerate_id()</a></span> with the destroy parameter set to <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>.
       Return value should be <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> for success, <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> for failure.
      </p>
     </dd>
    
    
     <dt><code class="parameter">gc</code></dt>
     <dd>
      <p class="para">
       A callable with the following signature:
       <div class="methodsynopsis dc-description"><span class="methodname"><span class="replaceable">gc</span></span>(<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$lifetime</code></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

      </p>
      <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.php#ini.session.gc-probability" class="link">session.gc_probability</a> and <a href="session.configuration.php#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.php#ini.session.gc-maxlifetime" class="link">session.gc_maxlifetime</a>.
       Return value should be <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> for success, <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> for failure.
      </p>
     </dd>
    
    
     <dt><code class="parameter">create_sid</code></dt>
     <dd>
      <p class="para">
       A callable with the following signature:
       <div class="methodsynopsis dc-description"><span class="methodname"><span class="replaceable">create_sid</span></span>(): <span class="type"><a href="language.types.string.php" class="type string">string</a></span></div>

      </p>
      <p class="para">
       This callback is executed when a new session ID is required. No
       parameters are provided, and the return value should be a string that
       is a valid session ID for your handler.
      </p>
     </dd>
    
    
     <dt><code class="parameter">validate_sid</code></dt>
     <dd>
      <p class="para">
       A callable with the following signature:
       <div class="methodsynopsis dc-description"><span class="methodname"><span class="replaceable">validate_sid</span></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$key</code></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

      </p>
      <p class="para">
       This callback is executed when a session is to be started, a session ID is supplied
       and <a href="session.configuration.php#ini.session.use-strict-mode" class="link">session.use_strict_mode</a> is enabled.
       The <code class="parameter">key</code> is the session ID to validate.
       A session ID is valid, if a session with that ID already exists.
       The return value should be <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> for success, <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> for failure.
      </p>
     </dd>
    
    
     <dt><code class="parameter">update_timestamp</code></dt>
     <dd>
      <p class="para">
       A callable with the following signature:
       <div class="methodsynopsis dc-description"><span class="methodname"><span class="replaceable">update_timestamp</span></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$key</code></span>, <span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$val</code></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

      </p>
      <p class="para">
       This callback is executed when a session is updated.
       <code class="parameter">key</code> is the session ID, <code class="parameter">val</code> is the session data.
       The return value should be <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> for success, <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> for failure.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.session-set-save-handler-returnvalues">
  <h3 class="title">Значення, що повертаються</h3>
  <p class="para">
   Повертає <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> у разі успіху або <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> в разі помилки.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.session-set-save-handler-examples">
  <h3 class="title">Приклади</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Приклад #1 
     Custom session handler: see full code in <span class="classname"><a href="class.sessionhandlerinterface.php" class="classname">SessionHandlerInterface</a></span> synopsis.
    </strong></p>
    <div class="example-contents"><p>
     We just show the invocation here, the full example can be
     seen in the <span class="classname"><a href="class.sessionhandlerinterface.php" class="classname">SessionHandlerInterface</a></span> synopsis 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 </span><span style="color: #0000BB">MySessionHandler </span><span style="color: #007700">implements </span><span style="color: #0000BB">SessionHandlerInterface<br /></span><span style="color: #007700">{<br />    </span><span style="color: #FF8000">// implement interfaces here<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #0000BB">$handler </span><span style="color: #007700">= new </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">, </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">// proceed to set and retrieve values by key from $_SESSION</span></span></code></div>
    </div>

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


 <div class="refsect1 notes" id="refsect1-function.session-set-save-handler-notes">
  <h3 class="title">Примітки</h3>
  <div class="warning"><strong class="warning">Увага</strong>
   <p class="para">
    The <code class="parameter">write</code> and
    <code class="parameter">close</code> 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.php" 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">Увага</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.php" class="function">session_write_close()</a></span>.
   </p>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.session-set-save-handler-seealso">
  <h3 class="title">Прогляньте також</h3>
  <p class="para">
   <ul class="simplelist">
    <li>
     The <a href="session.configuration.php#ini.session.save-handler" class="link">session.save_handler</a>
     configuration directive
    </li>
    <li>
     The <a href="session.configuration.php#ini.session.serialize-handler" class="link">session.serialize_handler</a>
     configuration directive.
    </li>
    <li>The <span class="function"><a href="function.register-shutdown-function.php" class="function" rel="rdfs-seeAlso">register_shutdown_function()</a> - Register a function for execution on shutdown</span></li>
    <li>The <span class="function"><a href="function.session-register-shutdown.php" class="function" rel="rdfs-seeAlso">session_register_shutdown()</a> - Session shutdown function</span></li>
    <li>
     Refer to <a href="https://github.com/php/php-src/blob/master/ext/session/tests/save_handler.inc" class="link external">&raquo;&nbsp;save_handler.inc</a>
     for a full procedural reference implementation
    </li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>