title: Not able to determine when session is active in PHP
body: <p>So I want to be able to tell, in PHP, whether a session is currently active. I thought that this would be easy by either using <code>session_status()</code> or <code>session_id()</code>, but both return inconclusive results.</p>
  
  <p>My PHP looks like this</p>
  
  <pre><code>if (session_id() == "") {
      echo json_encode("nope");
  } else {
      echo json_encode("yep");
  }
  
  if (session_status() === PHP_SESSION_DISABLED) {
      echo json_encode("disabled");
  } elseif (session_status() === PHP_SESSION_NONE) {
      echo json_encode("none");
  } else {
      echo json_encode("active");
  }
  
  $handler = new DatabaseSessionHandler();
  session_set_save_handler($handler, true);
  session_start();
  </code></pre>
  
  <p>And I have an Angular app making http calls to this script to basically activate its session.</p>
  
  <p>I've been testing this, and when I visit the site for the first time, no <code>session_id</code> is found and the <code>session_status()</code> returns back <code>"none"</code>, as they both should. But the problem is, when I refresh the page, and this PHP script is runs, but this time I have an active session (the PHP session cookie shows up in my browser), <code>session_id</code> is still acting like none exists. And also, <code>session_status()</code> returns back, mistakenly <code>"none"</code>.</p>
  
  <p>Any ideas would be greatly appreciated, thanks!</p>
  
  <p>Edit 1: A few people have mentioned that putting the <code>session_start()</code> in front of testing if it is active or not should work. When I do this, a <code>session_id</code> is always found, and the <code>session_status()</code> always returns back <code>"active"</code>. Even when a fresh new user visits the site, this still happens.</p>