title: Having trouble checking for existing session
body: <p>So I'm making a very rudimentary login system on my website, and I'm having trouble using session_status to check for an active session. Here's how it's going:</p>
  
  <p>I have a registration page which simultaneously acts as a form (when <code>$_SERVER['REQUEST_METHOD'] != 'POST'</code>) and a script to add the user to the database (when <code>$_SERVER['REQUEST_METHOD'] == 'POST'</code>). If everything goes well and the user is added, then I execute the following code:</p>
  
  <pre><code>session_start();
  $_SESSION['username'] = $username;
  $_SESSION['password'] = $password;
  
  header('Location: registration-successful.php');
  exit();
  </code></pre>
  
  <p>I know that my <code>session_start()</code> isn't at the beginning of the code, but that's okay because the program doesn't output anything before <code>session_start()</code> is called. (In fact, the only way <code>session_start()</code> can be called is if it never outputs anything, since it exits right after.)</p>
  
  <p>My registration-successful.php code looks like this:</p>
  
  <pre><code>&lt;?php
      if(session_status() == PHP_SESSION_ACTIVE){
          session_start();
      }
  ?&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
      &lt;meta charset="utf-8" /&gt;
      &lt;title&gt;Noether Here Nor There - Registration Successful&lt;/title&gt;
      &lt;link rel="stylesheet" href="css/main.css" type="text/css" /&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;h1&gt;Congratulations &lt;?php echo "{$_SESSION['username']}" ?&gt;, you were successfully registered!&lt;/h1&gt;
  &lt;p&gt;Continue on to &lt;a href="index.php"&gt;the main site&lt;/a&gt;.&lt;/p&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>As it stands, this code doesn't work. It instead prints out "Congratulations , you were successfully registered." That is, it's not recognizing the <code>$_SESSION</code>. So clearly the <code>session_start()</code> isn't being executed. So the if statement is failing.</p>
  
  <p>But I don't understand why. In fact, when I remove the <code>if(session_status() == PHP_SESSION_ACTIVE)</code> and simply put <code>session_start()</code>, it works.</p>
  
  <p>But I need to be able to use the <code>session_status() == PHP_SESSION_ACTIVE</code> because on my homepage, I use it to determine whether to display a login screen or the user's home page.</p>
  
  <p>Clearly, it's doable - after all, nearly every website (Facebook, for example) is able to display their login page if you're not logged in and display your home page if you are. So they must be checking for an active session somehow. I would use something like <code>isset($_SESSION[...])</code>, but</p>
  
  <p>a) I've been told that you can't reference <code>$_SESSION</code> variables before declaring <code>session_start()</code>, and
  b) The session_status function is supposed to eliminate the need for all that hacky stuff.</p>
  
  <p>Will someone please shed some light on this? I think I'm beginning to go mad.</p>
  
  <p>EDIT: Here's the code for my index.php page (which is linked to from registration-successful.php):</p>
  
  <pre><code>&lt;?php
      if(session_status() !== PHP_SESSION_ACTIVE){
      $active = FALSE;
      } else{
          $active = TRUE;
          session_start();
      }
  ?&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
      &lt;meta charset="utf-8" /&gt;
      &lt;title&gt;Title&lt;/title&gt;
      &lt;link rel="stylesheet" href="css/main.css" type="text/css" /&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;h1&gt;Title&lt;/h1&gt;
      &lt;?php
          if(!$active){
      ?&gt;
          &lt;a href="login.php"&gt;Log In&lt;/a&gt;
          &lt;br /&gt;
          &lt;a href="register.php"&gt;Register&lt;/a&gt;
      &lt;?php
          } else {
              echo "Welcome " . $_SESSION['username'] . ".";
  
          }
      ?&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>EDIT #2: Thanks guys, problem solved. I misunderstood what session_status was doing and when one would use it.
  The updated code simply puts session_start() at the top and then loads the appropriate stuff based on the condition "if(isset($_SESSION['username']))".</strong></p>