title: Checking a session variable from an include file - PHP
body: <p>When a user logs in to my system, I have a session variable <code>$_SESSION['logged_in'] = 1;</code> and then on each page I check the variable to see if the user is logged in or not, which works okay:</p>
  
  <pre><code>session_start();
  if (!$_SESSION['logged_in']) {
     header("location: https://mydomain.com/cpanel/login.php");
     exit();
  }
  </code></pre>
  
  <p>But now, for ease of maintenance, I want to add this session checker to an include file and include it at the top of every secure page. However, each time I log in it fails.</p>
  
  <p>This is what is in the include file:</p>
  
  <pre><code>// start session
  session_start();
  
  // check login session
  if (!$_SESSION['logged_in']) {
     header("location: https://mydomain.com/cpanel/login.php");
     exit();
  }
  </code></pre>
  
  <p>And this is what I have added to the header of each page:</p>
  
  <pre><code>// load authentication file
  include "../includes/authentication/check.php";
  </code></pre>
  
  <p>But it appears that the <code>$_SESSION</code> is empty, even though I am definitely logged in. I tried removing the <code>session_start();</code> from the include file as I thought that might be reetting it somehow - but that wasn't it.</p>
  
  <p>Have any of you guys got a clue as to what is going wrong here?</p>