<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/session.examples.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'session.customhandler.php',
    1 => 'Custom Session Handlers',
  ),
  'up' => 
  array (
    0 => 'session.examples.php',
    1 => 'Examples',
  ),
  'prev' => 
  array (
    0 => 'session.idpassing.php',
    1 => 'Passing the Session ID',
  ),
  'next' => 
  array (
    0 => 'session.upload-progress.php',
    1 => 'Session Upload Progress',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/session/examples.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="session.customhandler" class="section">
  <h2 class="title">Custom Session Handlers</h2>
  <p class="para">
   To implement database storage, or any other storage method, you
   will need to use <span class="function"><a href="function.session-set-save-handler.php" class="function">session_set_save_handler()</a></span> to
   create a set of user-level storage functions. A session handlers may be created
   using the <span class="classname"><a href="class.sessionhandlerinterface.php" class="classname">SessionHandlerInterface</a></span> or extending PHP&#039;s internal handlers by inheriting
   from <span class="classname"><a href="class.sessionhandler.php" class="classname">SessionHandler</a></span>.
  </p>
  <p class="para">
   The callbacks specified in <span class="function"><a href="function.session-set-save-handler.php" class="function">session_set_save_handler()</a></span> are methods
   called by PHP during the life-cycle of a session: <code class="parameter">open</code>, <code class="parameter">read</code>,
   <code class="parameter">write</code> and <code class="parameter">close</code> and for the housekeeping tasks:
   <code class="parameter">destroy</code> for deleting a session and <code class="parameter">gc</code> for periodic garbage
   collection.
  </p>
  <p class="para">
   Therefore, PHP always requires session save handlers. The default is usually the
   internal &#039;files&#039; save handler. A custom save handler can be set using
   <span class="function"><a href="function.session-set-save-handler.php" class="function">session_set_save_handler()</a></span>. Alternative internal save handlers are also
   provided by PHP extensions, such as <code class="parameter">sqlite</code>,
   <code class="parameter">memcache</code> and <code class="parameter">memcached</code> and can be set with
   <a href="session.configuration.php#ini.session.save-handler" class="link">session.save_handler</a>.
  </p>
  <p class="para">
   When the session starts, PHP will internally call the <code class="parameter">open</code> handler followed by the
   <code class="parameter">read</code> callback which should return an encoded string exactly as it was originally
   passed for storage. Once the <code class="parameter">read</code> callback returns the encoded string, PHP will
   decode it and then populate the resulting array into the <var class="varname"><a href="reserved.variables.session.php" class="classname">$_SESSION</a></var> superglobal.
  </p>
  <p class="para">
   When PHP shuts down (or when <span class="function"><a href="function.session-write-close.php" class="function">session_write_close()</a></span> is called),
   PHP will internally encode the <var class="varname"><a href="reserved.variables.session.php" class="classname">$_SESSION</a></var> superglobal and pass this
   along with the session ID to the <code class="parameter">write</code> callback.
   After the <code class="parameter">write</code> callback has finished, PHP will internally invoke the
   <code class="parameter">close</code> callback handler.
  </p>
  <p class="para">
   When a session is specifically destroyed, PHP will call the <code class="parameter">destroy</code> handler with
   the session ID.
  </p>
 <p class="para">
   PHP will call the <code class="parameter">gc</code> callback from time to time to
   expire any session records according to the set max lifetime of a session.
   This routine should delete all records from persistent storage which were
   last accessed longer than the <code class="parameter">$lifetime</code>.
 </p>
 </div><?php manual_footer($setup); ?>