title: When and where should I use session_start?
body: <p>Exactly when and where should I use <code>session_start()</code> in PHP?</p>
  
  <p>For example, say I have a login script that sets a session variable to tell whether or not the user is logged in. Must I then put the <code>session_start()</code> at the top of the script, or only right before I actually set the session variable if the login was successful?</p>
  
  <pre><code>&lt;?php
  // session_start(); here?
  
  if (login($username, $password)) {
      // session_start(); or here?
  
      $_SESSION["username"] = $username;
  }
  ?&gt;
  </code></pre>
  
  <p>Another case is this, according to w3schools</p>
  
  <blockquote>
    <p><strong>Note:</strong> The session_start() function must be the very first thing in your document. Before any HTML tags.</p>
  </blockquote>