title: How session_start() function works?
body: <p>Please, explain how <code>session_start()</code> function works.</p>
  
  <p>I don’t understand the sequence of actions when starting a session in php. Try to explain.</p>
  
  <p>HTTP is a client – server architecture. It means that the browser sends its request, the sever processes the request and sends back its answer. Each of these actions has appropriate headers.</p>
  
  <p>I checked (with <code>headers_list()</code>) what headers are sent back by the server with its answer when I want to start session. And among others there is header</p>
  
  <pre><code>Set-Cookie: PHPSESSID=7f4cbf53fbcd4717792447f32da7dba8
  </code></pre>
  
  <p>It seems that everything is ok, the server gives order to the browser to set the cookie.</p>
  
  <p>But.To start session I have to include the <code>session_start()</code> function in the beginning of the code of the page. So this function is started when the browser begins parsing the page. The browser meets the php opening tag <code>&lt;?php</code> followed by the <code>session_start()</code> function. Immediately it delegates control to the server. And the server only now starts the function. Only when it has already sent the page to the browser with all the headers.</p>
  
  <p>So I don’t understand how server can send <code>Set-Cookie</code> header before the browser starts parsing the page and meets the <code>session_start()</code> function? How it knows that it has to put <code>Set-Cookie</code> header before the command <code>session_start()</code> is executed? Or I misunderstand the process?</p>