<?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 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.session-start.php',
    1 => 'session_start',
    2 => 'Start new or resume existing session',
  ),
  'up' => 
  array (
    0 => 'ref.session.php',
    1 => 'Session Functions',
  ),
  'prev' => 
  array (
    0 => 'function.session-set-save-handler.php',
    1 => 'session_set_save_handler',
  ),
  'next' => 
  array (
    0 => 'function.session-status.php',
    1 => 'session_status',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/session/functions/session-start.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.session-start" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">session_start</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">session_start</span> &mdash; <span class="dc-title">Start new or resume existing session</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.session-start-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>session_start</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter">$options</code><span class="initializer"> = []</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_start()</strong></span> creates a session or resumes the
   current one based on a session identifier passed via a GET or POST
   request, or passed via a cookie.
  </p>
  <p class="para">
   When <span class="function"><strong>session_start()</strong></span> is called or when a session auto starts,
   PHP will call the open and read session save handlers.  These will either be a built-in
   save handler provided by default or by PHP extensions (such as SQLite or Memcached); or can be
   custom handler as defined by <span class="function"><a href="function.session-set-save-handler.php" class="function">session_set_save_handler()</a></span>.
   The read callback will retrieve any existing session data (stored in a special serialized format)
   and will be unserialized and used to automatically populate the $_SESSION superglobal when the
   read callback returns the saved session data back to PHP session handling.
  </p>
  <p class="para">
   To use a named session, call
   <span class="function"><a href="function.session-name.php" class="function">session_name()</a></span> before calling
   <span class="function"><strong>session_start()</strong></span>.
  </p>
  <p class="para">
   When <a href="session.configuration.php#ini.session.use-trans-sid" class="link">session.use_trans_sid</a>
   is enabled, the <span class="function"><strong>session_start()</strong></span> function will
   register an internal output handler for URL rewriting.
  </p>
  <p class="para">
   If a user uses <code class="literal">ob_gzhandler</code> or similar with
   <span class="function"><a href="function.ob-start.php" class="function">ob_start()</a></span>, the function order is important for
   proper output.  For example,
   <code class="literal">ob_gzhandler</code> must be registered before starting the session.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.session-start-parameters">
  <h3 class="title">Parameters</h3>
  <dl>
   
    <dt><code class="parameter">options</code></dt>
    <dd>
     <p class="para">
      If provided, this is an associative array of options that will override
      the currently set
      <a href="session.configuration.php" class="link">session configuration directives</a>.
      The keys should not include the <code class="literal">session.</code> prefix.
     </p>
     <p class="para">
      In addition to the normal set of configuration directives, a
      <code class="literal">read_and_close</code> option may also be provided. If set to
      <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>, this will result in the session being closed immediately after
      being read, thereby avoiding unnecessary locking if the session data
      won&#039;t be changed.
     </p>
    </dd>
   
  </dl>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.session-start-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   This function returns <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> if a session was successfully started,
   otherwise <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>.
  </p>
 </div>


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

     </thead>

     <tbody class="tbody">
      <tr>
       <td>7.1.0</td>
       <td>
        <span class="function"><strong>session_start()</strong></span> now returns <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> and no longer
        initializes <var class="varname"><a href="reserved.variables.session.php" class="classname">$_SESSION</a></var> when it failed to start the
        session.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.session-start-examples">
  <h3 class="title">Examples</h3>
  <div class="refsect2 unknown-34" id="refsect2-function.session-start-unknown-34">
   <h4 class="title">A basic session example</h4>

   <p class="para">
    <div class="example" id="example-1">
     <p><strong>Example #1 <var class="filename">page1.php</var></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">// page1.php<br /><br /></span><span style="color: #0000BB">session_start</span><span style="color: #007700">();<br /><br />echo </span><span style="color: #DD0000">'Welcome to page #1'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'favcolor'</span><span style="color: #007700">] = </span><span style="color: #DD0000">'green'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'animal'</span><span style="color: #007700">]   = </span><span style="color: #DD0000">'cat'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'time'</span><span style="color: #007700">]     = </span><span style="color: #0000BB">time</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// Works if session cookie was accepted<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">'&lt;br /&gt;&lt;a href="page2.php"&gt;page 2&lt;/a&gt;'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Or maybe pass along the session id, if needed<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">'&lt;br /&gt;&lt;a href="page2.php?' </span><span style="color: #007700">. </span><span style="color: #0000BB">SID </span><span style="color: #007700">. </span><span style="color: #DD0000">'"&gt;page 2&lt;/a&gt;'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>
   </p>
   <p class="para">
    After viewing <var class="filename">page1.php</var>, the second page
    <var class="filename">page2.php</var> will magically contain the session
    data.  Read the <a href="ref.session.php" class="link">session reference</a>
    for information on <a href="session.idpassing.php" class="link">propagating
    session ids</a> as it, for example, explains what the constant
    <strong><code><a href="session.constants.php#constant.sid">SID</a></code></strong> is all about.
   </p>
   <p class="para">
    <div class="example" id="example-2">
     <p><strong>Example #2 <var class="filename">page2.php</var></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">// page2.php<br /><br /></span><span style="color: #0000BB">session_start</span><span style="color: #007700">();<br /><br />echo </span><span style="color: #DD0000">'Welcome to page #2&lt;br /&gt;'</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'favcolor'</span><span style="color: #007700">]; </span><span style="color: #FF8000">// green<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'animal'</span><span style="color: #007700">];   </span><span style="color: #FF8000">// cat<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y m d H:i:s'</span><span style="color: #007700">, </span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'time'</span><span style="color: #007700">]);<br /><br /></span><span style="color: #FF8000">// You may want to use SID here, like we did in page1.php<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">'&lt;br /&gt;&lt;a href="page1.php"&gt;page 1&lt;/a&gt;'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

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

  <div class="refsect2 unknown-37" id="refsect2-function.session-start-unknown-37">
   <h4 class="title">Providing options to <span class="function"><strong>session_start()</strong></span></h4>

   <div class="example" id="example-3">
    <p><strong>Example #3 Overriding the cookie lifetime</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">// This sends a persistent cookie that lasts a day.<br /></span><span style="color: #0000BB">session_start</span><span style="color: #007700">([<br />    </span><span style="color: #DD0000">'cookie_lifetime' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">86400</span><span style="color: #007700">,<br />]);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

   <div class="example" id="example-4">
    <p><strong>Example #4 Reading the session and closing it</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">// If we know we don't need to change anything in the<br />// session, we can just read and close rightaway to avoid<br />// locking the session file and blocking other pages<br /></span><span style="color: #0000BB">session_start</span><span style="color: #007700">([<br />    </span><span style="color: #DD0000">'cookie_lifetime' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">86400</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">'read_and_close'  </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">true</span><span style="color: #007700">,<br />]);</span></span></code></div>
    </div>

   </div>
  </div>

 </div>


 <div class="refsect1 notes" id="refsect1-function.session-start-notes">
  <h3 class="title">Notes</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    To use cookie-based sessions, <span class="function"><strong>session_start()</strong></span>
    must be called before outputting anything to the browser.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Use of <a href="zlib.configuration.php#ini.zlib.output-compression" class="link">zlib.output_compression</a>
    is recommended instead of <span class="function"><a href="function.ob-gzhandler.php" class="function">ob_gzhandler()</a></span>
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    This function sends out several HTTP headers depending on the
    configuration. See <span class="function"><a href="function.session-cache-limiter.php" class="function">session_cache_limiter()</a></span> to
    customize these headers.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.session-start-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><var class="varname"><a href="reserved.variables.session.php" class="classname">$_SESSION</a></var></li>
    <li>
     The <a href="session.configuration.php#ini.session.auto-start" class="link">session.auto_start</a>
     configuration directive
    </li>
    <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>
   </ul>
  </p>
 </div>


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