title: PHP Multiple User Login Form - Navigation to Different Pages Based on Login Credentials
body: <p>I am trying to create a login page that will send the user to a different index.php page based on their login credentials. For example, should a user with the "IT Technician" role log in, they will be sent to "index.php", and if a user with the "Student" role log in, they will be sent to the "student/index.php" page.</p>
  
  <p>I can't see what's wrong with my code, but it's not working... I'm getting the "wrong login credentials" message every time I press the login button.</p>
  
  <p>My code for the user login page is here:</p>
  
  <pre><code>&lt;?php
  session_start();
  if (isset($_SESSION["manager"])) {
  header("location: http://www.zuluirminger.com/SchoolAdmin/index.php");
  exit();
  }
  ?&gt;
  
  &lt;?php
  if (isset($_POST["username"]) &amp;&amp; isset($_POST["password"]) &amp;&amp; isset($_POST["role"])) {
  $manager = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["username"]);
  $password = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["password"]);
  $role = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["role"]);
  include "adminscripts/connect_to_mysql.php";
  $sql = mysql_query("SELECT id FROM Users WHERE username='$manager' AND password='$password' AND role='$role' LIMIT 1");
  $existCount = mysql_num_rows($sql);
  if (($existCount == 1) &amp;&amp; ($role == 'IT Technician')) {
      while ($row = mysql_fetch_array($sql)) {
          $id = $row["id"];
      }
      $_SESSION["id"] = $id;
      $_SESSION["manager"] = $manager;
      $_SESSION["password"] = $password;
      $_SESSION["role"] = $role;
      header("location: http://www.zuluirminger.com/SchoolAdmin/index.php");
  } else {
      echo 'Your login details were incorrect. Please try again &lt;a href="http://www.zuluirminger.com/SchoolAdmin/index.php"&gt;here&lt;/a&gt;';
      exit();
  }
  }
  ?&gt;
  
  &lt;?php
  if (isset($_POST["username"]) &amp;&amp; isset($_POST["password"]) &amp;&amp; isset($_POST["role"])) {
  $manager = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["username"]);
  $password = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["password"]);
  $role = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["role"]);
  include "adminscripts/connect_to_mysql.php";
  $sql = mysql_query("SELECT id FROM Users WHERE username='$manager' AND password='$password' AND role='$role' LIMIT 1");
  $existCount = mysql_num_rows($sql);
  if (($existCount == 1) &amp;&amp; ($role == 'Student')) {
      while ($row = mysql_fetch_array($sql)) {
          $id = $row["id"];
      }
      $_SESSION["id"] = $id;
      $_SESSION["manager"] = $manager;
      $_SESSION["password"] = $password;
      $_SESSION["role"] = $role;
      header("location: http://www.zuluirminger.com/SchoolAdmin/student/index.php");
  } else {
      echo 'Your login details were incorrect. Please try again &lt;a href="http://www.zuluirminger.com/SchoolAdmin/index.php"&gt;here&lt;/a&gt;';
      exit();
  }
  }
  ?&gt;
  </code></pre>
  
  <p>And the form that the data is pulled from is shown here:</p>
  
  <pre><code>&lt;form id="LoginForm" name="LoginForm" method="post" action="http://www.zuluirminger.com/SchoolAdmin/user_login.php"&gt;
    User Name:&lt;br /&gt;
    &lt;input type="text" name="username" id="username" size="50" /&gt;&lt;br /&gt;
    &lt;br /&gt;
  
    Password:&lt;br /&gt;
    &lt;input type="password" name="password" id="password" size="50" /&gt;&lt;br /&gt;
    &lt;br /&gt;
  
    Log in as:
    &lt;select name="role" id="role"&gt;
      &lt;option value=""&gt;...&lt;/option&gt;
  &lt;option value="Head"&gt;Head&lt;/option&gt; 
  &lt;option value="Deputy Head"&gt;Deputy Head&lt;/option&gt; 
  &lt;option value="IT Technician"&gt;IT Technician&lt;/option&gt; 
  &lt;option value="Pastoral Care"&gt;Pastoral Care&lt;/option&gt; 
  &lt;option value="Bursar"&gt;Bursar&lt;/option&gt; 
  &lt;option value="Secretary"&gt;Secretary&lt;/option&gt; 
  &lt;option value="Housemaster"&gt;Housemaster&lt;/option&gt; 
  &lt;option value="Teacher"&gt;Teacher&lt;/option&gt; 
  &lt;option value="Tutor"&gt;Tutor&lt;/option&gt; 
  &lt;option value="Sanatorium Staff"&gt;Sanatorium Staff&lt;/option&gt; 
  &lt;option value="Kitchen Staff"&gt;Kitchen Staff&lt;/option&gt; 
  &lt;option value="Parent"&gt;Parent&lt;/option&gt; 
  &lt;option value="Student"&gt;Student&lt;/option&gt;
  &lt;/select&gt;&lt;br /&gt;
    &lt;br /&gt;
  
    &lt;input type="submit" name = "button" id="button" value="Log In" onclick="javascript:return validateLoginForm();" /&gt;
    &lt;/h3&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Once logged in (and should the correct page be loaded, the validation code I have at the top of the script looks like this:</p>
  
  <pre><code>&lt;?php
  session_start();
  if (!isset($_SESSION["manager"])) {
  header("location: http://www.zuluirminger.com/SchoolAdmin/user_login.php");
  exit();
  }
  
  $managerID = preg_replace('#[^0-9]#i', '', $_SESSION["id"]);
  $manager = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["manager"]);
  $password = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["password"]);
  $role = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["role"]);
  
  include "adminscripts/connect_to_mysql.php";
  $sql = mysql_query("SELECT id FROM Users WHERE username='$manager' AND password='$password' AND role='$role' LIMIT 1");
  $existCount = mysql_num_rows($sql);
  if ($existCount == 0) {
  header("location: http://www.zuluirminger.com/SchoolAdmin/index.php");
  exit();
  }
  ?&gt;
  </code></pre>
  
  <p>Just so you're aware, the database table has the following fields: <strong>id</strong>, <strong>username</strong>, <strong>password</strong> and <strong>role</strong>.</p>
  
  <p>Any help would be greatly appreciated!</p>
  
  <p>Many thanks,
  Zulu</p>