title: Only allow certain user accounts onto a page
body: <p>I am looking for a method to find an account, check if it holds a certain variable &amp; then allow it access to a page (basically like an admin-only page).
  This project is like a School CMS System, and so I want to make it so only teachers can actually access the page so they can add more students, etc.</p>
  <p>Here's what I have tried already:</p>
  <pre><code>if( $_SESSION['teacher']=='1'){
  (then show any other users another page:)
      header(&quot;Location: demo.com/home.php&quot;);
     }
  </code></pre>
  <p>(imagine demo.com is a real domain)</p>
  <p>This snippet actually lets every user onto the page and not just teachers - so it does not work for me.</p>
  <p>From this snippet, I am trying to let only people that have '1' on their teacher section in the database get onto the page.
  I am using 1 for teachers and 0 for students.</p>
  <p>Database Image:</p>
  <p><img src="https://i.stack.imgur.com/480nJ.png" alt="Database Image" /></p>
  <p>Basically I only want users that are teachers to view this page.</p>
  <p>I am happy to expand more on the topic &amp; share more images on the database structure &amp; login system - but my main question is 'How do I restrict access to a page?'</p>
  <p>Code to start a session:</p>
  <pre><code>&lt;?php
  session_start();
  require_once 'dbconnect.php';
  
  if (isset($_SESSION['userSession'])!=&quot;&quot;) {
      header(&quot;Location: home.php&quot;);
      exit;
  }
  
  if (isset($_POST['btn-login'])) {
      
      $email = strip_tags($_POST['email']);
      $password = strip_tags($_POST['password']);
      
      $email = $DBcon-&gt;real_escape_string($email);
      $password = $DBcon-&gt;real_escape_string($password);
      
      $query = $DBcon-&gt;query(&quot;SELECT user_id, email, password FROM tbl_users WHERE email='$email'&quot;);
      $row=$query-&gt;fetch_array();
      
      $count = $query-&gt;num_rows; // if email/password are correct returns must be 1 row
      
      if (password_verify($password, $row['password']) &amp;&amp; $count==1) {
          $_SESSION['userSession'] = $row['user_id'];
          header(&quot;Location: home.php&quot;);
      } else {
          $msg = &quot;&lt;div class='alert alert-danger'&gt;
                      &lt;span class='glyphicon glyphicon-info-sign'&gt;&lt;/span&gt; &amp;nbsp; Invalid Username or Password !
                  &lt;/div&gt;&quot;;
      }
      $DBcon-&gt;close();
  }
  ?&gt;
  </code></pre>
  <p>Check if session is open/exists code:</p>
  <pre><code>&lt;?php
  session_start();
  include_once 'dbconnect.php';
  
  if (!isset($_SESSION['userSession'])) {
      header(&quot;Location: index.php&quot;);
  }
  
  $query = $DBcon-&gt;query(&quot;SELECT * FROM tbl_users WHERE user_id=&quot;.$_SESSION['userSession']);
  $userRow=$query-&gt;fetch_array();
  $DBcon-&gt;close();
  
  ?&gt;
  </code></pre>