<?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-regenerate-id.php',
    1 => 'session_regenerate_id',
    2 => 'Update the current session id with a newly generated one',
  ),
  'up' => 
  array (
    0 => 'ref.session.php',
    1 => 'Session Функції',
  ),
  'prev' => 
  array (
    0 => 'function.session-name.php',
    1 => 'session_name',
  ),
  'next' => 
  array (
    0 => 'function.session-register-shutdown.php',
    1 => 'session_register_shutdown',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/session/functions/session-regenerate-id.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.session-regenerate-id" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">session_regenerate_id</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.3.2, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">session_regenerate_id</span> &mdash; <span class="dc-title">
   Update the current session id with a newly generated one
  </span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.session-regenerate-id-description">
  <h3 class="title">Опис</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>session_regenerate_id</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> <code class="parameter">$delete_old_session</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.false">false</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_regenerate_id()</strong></span> will replace the current
   session id with a new one, and keep the current session information.
  </p>
  <p class="para">
   When <a href="session.configuration.php#ini.session.use-trans-sid" class="link">session.use_trans_sid</a>
   is enabled, output must be started after <span class="function"><strong>session_regenerate_id()</strong></span>
   call. Otherwise, old session ID is used.
  </p>
  <div class="warning"><strong class="warning">Увага</strong>
   <p class="para">
    Currently, session_regenerate_id does not handle an unstable network well,
    e.g. Mobile and WiFi network. Therefore, you may experience a lost
    session by calling session_regenerate_id.
   </p>
   <p class="para">
    You should not destroy old session data immediately, but should use
    destroy time-stamp and control access to old session ID. Otherwise,
    concurrent access to page may result in inconsistent state, or you
    may have lost session, or it may cause client (browser) side race
    condition and may create many session ID needlessly. Immediate
    session data deletion disables session hijack attack detection
    and prevention also.
   </p>
  </div>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.session-regenerate-id-parameters">
  <h3 class="title">Параметри</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">delete_old_session</code></dt>
      <dd>
       <p class="para">
        Whether to delete the old associated session file or not.
        You should not delete old session if you need to avoid
        races caused by deletion or detect/avoid session hijack
        attacks.
       </p>
      </dd>
     
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.session-regenerate-id-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-regenerate-id-examples">
  <h3 class="title">Приклади</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Приклад #1 A <span class="function"><strong>session_regenerate_id()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// NOTE: This code is not fully working code, but an example!<br /><br /></span><span style="color: #0000BB">session_start</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// Check destroyed time-stamp<br /></span><span style="color: #007700">if (isset(</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'destroyed'</span><span style="color: #007700">])<br />    &amp;&amp; </span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'destroyed'</span><span style="color: #007700">] &lt; </span><span style="color: #0000BB">time</span><span style="color: #007700">() - </span><span style="color: #0000BB">300</span><span style="color: #007700">) {<br />    </span><span style="color: #FF8000">// Should not happen usually. This could be attack or due to unstable network.<br />    // Remove all authentication status of this users session.<br />    </span><span style="color: #0000BB">remove_all_authentication_flag_from_active_sessions</span><span style="color: #007700">(</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'userid'</span><span style="color: #007700">]);<br />    throw(new </span><span style="color: #0000BB">DestroyedSessionAccessException</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$old_sessionid </span><span style="color: #007700">= </span><span style="color: #0000BB">session_id</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// Set destroyed timestamp<br /></span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'destroyed'</span><span style="color: #007700">] = </span><span style="color: #0000BB">time</span><span style="color: #007700">(); </span><span style="color: #FF8000">// session_regenerate_id() saves old session data<br /><br />// Simply calling session_regenerate_id() may result in lost session, etc.<br />// See next example.<br /></span><span style="color: #0000BB">session_regenerate_id</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// New session does not need destroyed timestamp<br /></span><span style="color: #007700">unset(</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'destroyed'</span><span style="color: #007700">]);<br /><br /></span><span style="color: #0000BB">$new_sessionid </span><span style="color: #007700">= </span><span style="color: #0000BB">session_id</span><span style="color: #007700">();<br /><br />echo </span><span style="color: #DD0000">"Old Session: </span><span style="color: #0000BB">$old_sessionid</span><span style="color: #DD0000">&lt;br /&gt;"</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"New Session: </span><span style="color: #0000BB">$new_sessionid</span><span style="color: #DD0000">&lt;br /&gt;"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>

  <p class="para">
   Current session module does not handle unstable network well. You should
   manage session ID to avoid lost session by session_regenerate_id.
  </p>

  <p class="para">
   <div class="example" id="example-2">
    <p><strong>Приклад #2 Avoiding lost session by <span class="function"><strong>session_regenerate_id()</strong></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// NOTE: This code is not fully working code, but an example!<br />// my_session_start() and my_session_regenerate_id() avoid lost sessions by<br />// unstable network. In addition, this code may prevent exploiting stolen<br />// session by attackers.<br /><br /></span><span style="color: #007700">function </span><span style="color: #0000BB">my_session_start</span><span style="color: #007700">() {<br />    </span><span style="color: #0000BB">session_start</span><span style="color: #007700">();<br />    if (isset(</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'destroyed'</span><span style="color: #007700">])) {<br />       if (</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'destroyed'</span><span style="color: #007700">] &lt; </span><span style="color: #0000BB">time</span><span style="color: #007700">()-</span><span style="color: #0000BB">300</span><span style="color: #007700">) {<br />           </span><span style="color: #FF8000">// Should not happen usually. This could be attack or due to unstable network.<br />           // Remove all authentication status of this users session.<br />           </span><span style="color: #0000BB">remove_all_authentication_flag_from_active_sessions</span><span style="color: #007700">(</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'userid'</span><span style="color: #007700">]);<br />           throw(new </span><span style="color: #0000BB">DestroyedSessionAccessException</span><span style="color: #007700">);<br />       }<br />       if (isset(</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'new_session_id'</span><span style="color: #007700">])) {<br />           </span><span style="color: #FF8000">// Not fully expired yet. Could be lost cookie by unstable network.<br />           // Try again to set proper session ID cookie.<br />           // NOTE: Do not try to set session ID again if you would like to remove<br />           // authentication flag.<br />           </span><span style="color: #0000BB">session_commit</span><span style="color: #007700">();<br />           </span><span style="color: #0000BB">session_id</span><span style="color: #007700">(</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'new_session_id'</span><span style="color: #007700">]);<br />           </span><span style="color: #FF8000">// New session ID should exist<br />           </span><span style="color: #0000BB">session_start</span><span style="color: #007700">();<br />           return;<br />       }<br />   }<br />}<br /><br />function </span><span style="color: #0000BB">my_session_regenerate_id</span><span style="color: #007700">() {<br />    </span><span style="color: #FF8000">// New session ID is required to set proper session ID<br />    // when session ID is not set due to unstable network.<br />    </span><span style="color: #0000BB">$new_session_id </span><span style="color: #007700">= </span><span style="color: #0000BB">session_create_id</span><span style="color: #007700">();<br />    </span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'new_session_id'</span><span style="color: #007700">] = </span><span style="color: #0000BB">$new_session_id</span><span style="color: #007700">;<br />    <br />    </span><span style="color: #FF8000">// Set destroy timestamp<br />    </span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'destroyed'</span><span style="color: #007700">] = </span><span style="color: #0000BB">time</span><span style="color: #007700">();<br />    <br />    </span><span style="color: #FF8000">// Write and close current session;<br />    </span><span style="color: #0000BB">session_commit</span><span style="color: #007700">();<br /><br />    </span><span style="color: #FF8000">// Start session with new session ID<br />    </span><span style="color: #0000BB">session_id</span><span style="color: #007700">(</span><span style="color: #0000BB">$new_session_id</span><span style="color: #007700">);<br />    </span><span style="color: #0000BB">ini_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'session.use_strict_mode'</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">);<br />    </span><span style="color: #0000BB">session_start</span><span style="color: #007700">();<br />    </span><span style="color: #0000BB">ini_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'session.use_strict_mode'</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />    <br />    </span><span style="color: #FF8000">// New session does not need them<br />    </span><span style="color: #007700">unset(</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'destroyed'</span><span style="color: #007700">]);<br />    unset(</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'new_session_id'</span><span style="color: #007700">]);<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>

 </div>


 <div class="refsect1 seealso" id="refsect1-function.session-regenerate-id-seealso">
  <h3 class="title">Прогляньте також</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.session-id.php" class="function" rel="rdfs-seeAlso">session_id()</a> - Get and/or set the current session id</span></li>
    <li><span class="function"><a href="function.session-create-id.php" class="function" rel="rdfs-seeAlso">session_create_id()</a> - Create new session id</span></li>
    <li><span class="function"><a href="function.session-start.php" class="function" rel="rdfs-seeAlso">session_start()</a> - Start new or resume existing session</span></li>
    <li><span class="function"><a href="function.session-destroy.php" class="function" rel="rdfs-seeAlso">session_destroy()</a> - Destroys all data registered to a session</span></li>
    <li><span class="function"><a href="function.session-reset.php" class="function" rel="rdfs-seeAlso">session_reset()</a> - Re-initialize session array with original values</span></li>
    <li><span class="function"><a href="function.session-name.php" class="function" rel="rdfs-seeAlso">session_name()</a> - Get and/or set the current session name</span></li>
   </ul>
  </p>
 </div>


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