title: Login system with different users roles
body: <p>I did a login/register system and it works fine. I wanted to add user roles, e.g. When the admin logs in, he will be redirected to index page; when user logs in, he will be redirected to profile page.
  This is what I did:</p>
  
  <p>functions.php </p>
  
  <pre><code>    public function loginUser($username,$password){
      $query=$this-&gt;db-&gt;prepare("SELECT id, username FROM users WHERE username=? AND email=?");
      $query-&gt;execute(array($username,$password));
      $userdata=$query-&gt;fetch();
  
      $num=$query-&gt;rowCount();
  
      if($num==1){
          session_start();
          $_SESSION['login']= true;
          $_SESSION['uid']= $userdata['id'];
          $_SESSION['uname']= $userdata['username'];
          $_SESSION['login_msg']= "Login succesful";
  
          return true;
      }else{
          return false;
      }
  }
  
  public function userRole($uid){
      $query=$this-&gt;db-&gt;prepare("SELECT role FROM users WHERE id=?");
      $query-&gt;execute(array($uid));
      $res=$query-&gt;fetch();
      echo $res['role'];
  }
  </code></pre>
  
  <p>login.php</p>
  
  <pre><code>&lt;?php
  if($_SERVER['REQUEST_METHOD'] == 'POST'){
  $username=$_POST['username'];
  $password=$_POST['password'];
  
  if(empty($username) or empty($password)){
     echo "Error... Field must not be empty";
  }else{
     $login = $user-&gt;loginUser($username,$password);
     if($login){
        header('Location: transition.php');
     }else{
       echo "E-mail or password not match";
     }
   }
  }
  ?&gt;
  
              &lt;form action="" method="post" name="reg"&gt;
                  &lt;table&gt;
                      &lt;tr&gt;&lt;td&gt; &lt;input type="text" name="username" placeholder="Nombre de usuario"&gt;&lt;/td&gt;&lt;/tr&gt;
                      &lt;tr&gt;&lt;td&gt; &lt;input type="password" name="password" placeholder="Password"&gt;&lt;/td&gt;&lt;/tr&gt;
  
                      &lt;tr&gt;&lt;td&gt; &lt;input type="submit" name="login" value="Login" onclick="return(submitreg());"&gt;&lt;/td&gt;&lt;/tr&gt;
                  &lt;/table&gt;
              &lt;/form&gt;
  </code></pre>
  
  <p>transition.php</p>
  
  <pre><code>&lt;?php
  session_start();
  require_once "functions.php";
  $db = new DatabaseConnection();
  $user = new LoginRegister($db-&gt;pdo);
  
  $uid=$_SESSION['uid'];
  $username=$_SESSION['uname'];
  
  if(!$user-&gt;getSession()){
  header('Location: login.php');
  exit();
  }
  $type = $user-&gt;userRole($uid);
  echo $type;
  
  if($type == 0){
  header('Location: index.php');
  }else{header('Location: profile.php');
  }
  ?&gt; 
  </code></pre>
  
  <p>I added the function userRole to get the role of the user, and transition.php is to know if the function works fine, if I delete the if sentence and I print the role, it prints the right role.
  When I login without this function and php file it works properly, but when I added this function and php file, I always get redirected to index.php regardless the role.</p>
  
  <p>In my database the user role is just a number (0 for admin and 1 for user).</p>