title: Redirect to a page based on user roles in codeigniter
body: <p>Am working on a user login authentication with codeigniter.</p>
  
  <p>The login script works great. My question is, when a user is logged in I want the user to be redirected to a page based on his user roles</p>
  
  <p>Roles are as below <strong>subscriber</strong>, <strong>admin</strong>, <strong>client</strong>, <strong>postman</strong></p>
  
  <p><strong>User_model.php</strong> is as below</p>
  
  <pre><code>class User_model extends CI_Model {
  
      public $status;
      public $roles;
  
      function __construct(){
          // Call the Model constructor
          parent::__construct();
          $this-&gt;status = $this-&gt;config-&gt;item('status');
          $this-&gt;roles = $this-&gt;config-&gt;item('roles');
      }
  
      public function insertUser($d)
      {
              $string = array(
                  'firstname'=&gt;$d['firstname'],
                  'lastname'=&gt;$d['lastname'],
                  'phonenumber'=&gt;$d['phonenumber'],
                  'email'=&gt;$d['email'],
                  'roles'=&gt;$this-&gt;roles[1],
                  'status'=&gt;$this-&gt;status[1],
                  'password'=&gt; '',
                  'last_login'=&gt; '',
                  'created_at'=&gt; '',
                  'updated_at'=&gt; ''
              );
              $q = $this-&gt;db-&gt;insert_string('users',$string);
              $this-&gt;db-&gt;query($q);
              return $this-&gt;db-&gt;insert_id();
      }
  
      public function isDuplicate($email)
      {
          $this-&gt;db-&gt;get_where('users', array('email' =&gt; $email), 1);
          return $this-&gt;db-&gt;affected_rows() &gt; 0 ? TRUE : FALSE;
      }
  
      public function insertToken($user_id)
      {
          $token = substr(sha1(rand()), 0, 30);
          $date = date('Y-m-d');
  
          $string = array(
                  'token'=&gt; $token,
                  'user_id'=&gt;$user_id,
                  'created'=&gt;$date
              );
          $query = $this-&gt;db-&gt;insert_string('tokens',$string);
          $this-&gt;db-&gt;query($query);
          return $token . $user_id;
  
      }
  
      public function isTokenValid($token)
      {
         $tkn = substr($token,0,30);
         $uid = substr($token,30);
  
          $q = $this-&gt;db-&gt;get_where('tokens', array(
              'tokens.token' =&gt; $tkn,
              'tokens.user_id' =&gt; $uid), 1);
  
          if($this-&gt;db-&gt;affected_rows() &gt; 0){
              $row = $q-&gt;row();
  
              $created = $row-&gt;created;
              $createdTS = strtotime($created);
              $today = date('Y-m-d');
              $todayTS = strtotime($today);
  
              if($createdTS != $todayTS){
                  return false;
              }
  
              $user_info = $this-&gt;getUserInfo($row-&gt;user_id);
              return $user_info;
  
          }else{
              return false;
          }
  
      }
  
      public function getUserInfo($id)
      {
          $q = $this-&gt;db-&gt;get_where('users', array('id' =&gt; $id), 1);
          if($this-&gt;db-&gt;affected_rows() &gt; 0){
              $row = $q-&gt;row();
              return $row;
          }else{
              error_log('no user found getUserInfo('.$id.')');
              return false;
          }
      }
  
      public function updateUserInfo($post)
      {
          $data = array(
                 'password' =&gt; $post['password'],
                 'last_login' =&gt; date('Y-m-d h:i:s A'),
                 'created_at' =&gt; date('Y-m-d h:i:s A'),
                 'updated_at' =&gt; date('Y-m-d h:i:s A'),
                 'status' =&gt; $this-&gt;status[1]
              );
          $this-&gt;db-&gt;where('id', $post['user_id']);
          $this-&gt;db-&gt;update('users', $data);
          $success = $this-&gt;db-&gt;affected_rows();
  
          if(!$success){
              error_log('Unable to updateUserInfo('.$post['user_id'].')');
              return false;
          }
  
          $user_info = $this-&gt;getUserInfo($post['user_id']);
          return $user_info;
      }
  
      public function checkLogin($post)
      {
          $this-&gt;load-&gt;library('password');
          $this-&gt;db-&gt;select('*');
          $this-&gt;db-&gt;where('email', $post['email']);
          $query = $this-&gt;db-&gt;get('users');
          $userInfo = $query-&gt;row();
  
          if(!$this-&gt;password-&gt;validate_password($post['password'], $userInfo-&gt;password)){
              error_log('Unsuccessful login attempt('.$post['email'].')');
              return false;
          }
  
          $this-&gt;updateLoginTime($userInfo-&gt;id);
  
          unset($userInfo-&gt;password);
          return $userInfo;
      }
  
  }
  </code></pre>
  
  <p>The main code for controller is as seen below</p>
  
  <pre><code>defined('BASEPATH') OR exit('No direct script access allowed');
  
  class Main extends CI_Controller {
  
          public $status;
          public $roles;
  
          function __construct(){
              parent::__construct();
              $this-&gt;load-&gt;model('User_model', 'user_model', TRUE);
              $this-&gt;load-&gt;library('form_validation');
              $this-&gt;form_validation-&gt;set_error_delimiters('&lt;div class="error"&gt;', '&lt;/div&gt;');
              $this-&gt;status = $this-&gt;config-&gt;item('status');
              $this-&gt;roles = $this-&gt;config-&gt;item('roles');
          }
  
      public function index()
      {
  
              if(empty($this-&gt;session-&gt;userdata['email'])){
                  redirect(site_url().'main/login/');
              }
  
              /*front page*/
              $data = $this-&gt;session-&gt;userdata;
              $this-&gt;load-&gt;view('header');
              $this-&gt;load-&gt;view('index', $data);
              $this-&gt;load-&gt;view('footer');
      }
  
  
  
          public function login()
          {
  
              $this-&gt;form_validation-&gt;set_rules('email', 'Email', 'required|valid_email');
              $this-&gt;form_validation-&gt;set_rules('password', 'Password', 'required');
  
              if($this-&gt;form_validation-&gt;run() == FALSE) {
                  $this-&gt;load-&gt;view('header');
                  $this-&gt;load-&gt;view('login');
                  $this-&gt;load-&gt;view('footer');
              }else{
  
                  $post = $this-&gt;input-&gt;post();
                  $clean = $this-&gt;security-&gt;xss_clean($post);
  
                  $userInfo = $this-&gt;user_model-&gt;checkLogin($clean);
  
                  if(!$userInfo){
                      $this-&gt;session-&gt;set_flashdata('flash_message', 'The login was unsucessful');
                      redirect(site_url().'main/login');
                  }
                  foreach($userInfo as $key=&gt;$val){
                      $this-&gt;session-&gt;set_userdata($key, $val);
                  }
                  redirect(site_url().'main/');
              }
  
          }
  
  
          public function logout()
          {
              $this-&gt;session-&gt;sess_destroy();
              redirect(site_url().'main/login/');
          }
  
  
  }
  </code></pre>