title: PHP- How can I restrict access to a page?
body: <p>I have multiple pages that needs to be protected depending on the user privilege. I have a php to check the current session variable upon page load. </p>
  
  <p>page being tested; the php code is placed above the !DOCTYPE. this code is suppose to check for unlogged in customers. if not logged in or no session variable set redirect to error page otherwise do nothing and load page normally</p>
  
  <pre><code>&lt;?php
  if (!isset($_SESSION["username"])){
      header("location: error.php");
  }
  ?&gt;
  </code></pre>
  
  <p>my session variables are only set after logging in, after logging in the user is redirected to the page referred to above:</p>
  
  <pre><code>if (mysqli_num_rows($results6) &lt; 1) {  //$results6 is a query to check if the user exits in the users database
      $logInMsg = "invalid log in";
  } else {
      session_start();
      $_SESSION["username"] = $uName;  //$uName is a user input user name
      header("location: pageabove.php");
  }
  </code></pre>
  
  <p>the problem is that even after logging in I still get redirected to the error page</p>