title: codeigniter login page does not redirect to dashboard
body: <p>i have a problem in a controller file called <strong>"pages.php"</strong>. After login via login page with correct credentials pages.php does not open dashboard. I doubt that probably the source of this problem may be found in this <strong>pages.php</strong></p>
  
  <pre><code>&lt;?php 
  class Pages extends MY_Controller
  {
  public function view($page = 'login')
  {
      if (!file_exists(APPPATH.'views/'.$page.'.php'))
      {
          // Whoops, we don't have a page for that!
          show_404();
      }
  
      $data['title'] = ucfirst($page); // Capitalize the first letter
  
      if($page == 'section' || $page == 'subject' || $page == 'student' || $page == 'marksheet' || $page == 'accounting') {
          $this-&gt;load-&gt;model('model_classes');
          $data['classData'] = $this-&gt;model_classes-&gt;fetchClassData();
  
          $this-&gt;load-&gt;model('model_teacher');
          $data['teacherData'] = $this-&gt;model_teacher-&gt;fetchTeacherData();
  
  
          $this-&gt;load-&gt;model('model_accounting');
          $data['totalIncome'] = $this-&gt;model_accounting-&gt;totalIncome();
          $data['totalExpenses'] = $this-&gt;model_accounting-&gt;totalExpenses();
          $data['totalBudget'] = $this-&gt;model_accounting-&gt;totalBudget();
      }
  
      if($page == 'setting') {
          $this-&gt;load-&gt;model('model_users');
          $this-&gt;load-&gt;library('session');
          $userId = $this-&gt;session-&gt;userdata('id');
          $data['userData'] = $this-&gt;model_users-&gt;fetchUserData($userId);
      }
  
      if($page == 'dashboard') {
          $this-&gt;load-&gt;model('model_student');
          $this-&gt;load-&gt;model('model_teacher');
          $this-&gt;load-&gt;model('model_classes');
          $this-&gt;load-&gt;model('model_marksheet');
          $this-&gt;load-&gt;model('model_accounting');
  
          $data['countTotalStudent'] = $this-&gt;model_student-&gt;countTotalStudent();
          $data['countTotalTeacher'] = $this-&gt;model_teacher-&gt;countTotalTeacher();
          $data['countTotalClasses'] = $this-&gt;model_classes-&gt;countTotalClass();
          $data['countTotalMarksheet'] = $this-&gt;model_marksheet-&gt;countTotalMarksheet();
  
          $data['totalIncome'] = $this-&gt;model_accounting-&gt;totalIncome();
          $data['totalExpenses'] = $this-&gt;model_accounting-&gt;totalExpenses();
          $data['totalBudget'] = $this-&gt;model_accounting-&gt;totalBudget();
      }
  
      if($page == 'login') {
          $this-&gt;isLoggedIn();
          $this-&gt;load-&gt;view($page, $data);
      } 
      else{
          $this-&gt;isNotLoggedIn();
  
          $this-&gt;load-&gt;view('templates/header', $data);
          $this-&gt;load-&gt;view($page, $data);    
          $this-&gt;load-&gt;view('templates/footer', $data);    
      }
  }
  
  }
  </code></pre>
  
  <p>Please anyone who can edit this code just help. Or if you think the problem is not in <strong>pages.php</strong> (above page) please advice me how the problem can be solved. I have mentioned more pages:-</p>
  
  <p>This is <strong>rootes.php.</strong></p>
  
  <pre><code>&lt;?php
  defined('BASEPATH') OR exit('No direct script access allowed');
  $route['(:any)'] = 'pages/view/$1';
  $route['default_controller'] = 'pages/view';
  
  
  $route['404_override'] = '';
  $route['translate_uri_dashes'] = FALSE;
  </code></pre>
  
  <p>This is <strong>MY_Controller.php</strong> in core folder</p>
  
  <pre><code>&lt;?php 
  class MY_Controller extends CI_Controller 
  {
  public function __construct()
  {
      parent::__construct();
  }
  
  public function isLoggedIn()
  {
  
      $this-&gt;load-&gt;library('session');
  
      if($this-&gt;session-&gt;userdata('logged_in') === true) {
          redirect('../dashboard');
      }
  }   
  
  public function isNotLoggedIn()
  {
  
      $this-&gt;load-&gt;library('session');
  
      if($this-&gt;session-&gt;userdata('logged_in') != true) {
          redirect('../../');
      }
  }
  
  }
  </code></pre>
  
  <p>This is <strong>Users.php</strong> in controllers folder</p>
  
  <pre><code>&lt;?php 
  class Users extends MY_Controller
  {
  public function __construct()
  {
      parent::__construct();
  
      // loading the users model
      $this-&gt;load-&gt;model('model_users');
  
      // loading the form validation library
      $this-&gt;load-&gt;library('form_validation');        
  
  }
  
  public function login()
  {
  
      $validator = array('success' =&gt; false, 'messages' =&gt; array());
  
      $validate_data = array(
          array(
              'field' =&gt; 'username',
              'label' =&gt; 'Username',
              'rules' =&gt; 'required|callback_validate_username'
          ),
          array(
              'field' =&gt; 'password',
              'label' =&gt; 'Password',
              'rules' =&gt; 'required'
          )
      );
  
      $this-&gt;form_validation-&gt;set_rules($validate_data);
      $this-&gt;form_validation-&gt;set_error_delimiters('&lt;p class="text-danger"&gt;','&lt;/p&gt;');
  
      if($this-&gt;form_validation-&gt;run() === true) {            
          $username = $this-&gt;input-&gt;post('username');
          $password = md5($this-&gt;input-&gt;post('password'));
  
          $login = $this-&gt;model_users-&gt;login($username, $password);
  
          if($login) {
              $this-&gt;load-&gt;library('session');
  
              $user_data = array(
                  'id' =&gt; $login,
                  'logged_in' =&gt; true
              );
  
              $this-&gt;session-&gt;set_userdata($user_data);
  
              $validator['success'] = true;
              $validator['messages'] = "index.php/dashboard";             
          }   
          else {
              $validator['success'] = false;
              $validator['messages'] = "Incorrect username/password combination";
          } // /else
  
      }   
      else {
          $validator['success'] = false;
          foreach ($_POST as $key =&gt; $value) {
              $validator['messages'][$key] = form_error($key);
          }           
      } // /else
  
      echo json_encode($validator);
  } // /lgoin function
  
  public function validate_username()
  {
      $validate = $this-&gt;model_users-&gt;validate_username($this-&gt;input-&gt;post('username'));
  
      if($validate === true) {
          return true;
      } 
      else {
          $this-&gt;form_validation-&gt;set_message('validate_username', 'The {field} does not exists');
          return false;           
      } // /else
  } // /validate username function
  
  public function logout()
  {
      $this-&gt;load-&gt;library('session');
      $this-&gt;session-&gt;sess_destroy();
      redirect('../../');
  }
  
  
  public function updateProfile()
  {
      $this-&gt;load-&gt;library('session');
      $userId = $this-&gt;session-&gt;userdata('id');
  
      $validator = array('success' =&gt; false, 'messages' =&gt; array());
  
      $validate_data = array(
          array(
              'field' =&gt; 'username',
              'label' =&gt; 'Username',
              'rules' =&gt; 'required'
          ),
          array(
              'field' =&gt; 'fname',
              'label' =&gt; 'First Name',
              'rules' =&gt; 'required'
          )
      );
  
      $this-&gt;form_validation-&gt;set_rules($validate_data);
      $this-&gt;form_validation-&gt;set_error_delimiters('&lt;p class="text-danger"&gt;','&lt;/p&gt;');
  
      if($this-&gt;form_validation-&gt;run() === true) {    
          $update = $this-&gt;model_users-&gt;updateProfile($userId);                   
          if($update === true) {
              $validator['success'] = true;
              $validator['messages'] = "Successfully Update";
          }
          else {
              $validator['success'] = false;
              $validator['messages'] = "Error while inserting the information into the database";
          }           
      }   
      else {
          $validator['success'] = false;
          foreach ($_POST as $key =&gt; $value) {
              $validator['messages'][$key] = form_error($key);
          }           
      } // /else
  
      echo json_encode($validator);
  }
  
  public function changePassword()
  {
      $this-&gt;load-&gt;library('session');
      $userId = $this-&gt;session-&gt;userdata('id');
  
      $validator = array('success' =&gt; false, 'messages' =&gt; array());
  
      $validate_data = array(
          array(
              'field' =&gt; 'currentPassword',
              'label' =&gt; 'Current Password',
              'rules' =&gt; 'required|callback_validate_current_password'
          ),
          array(
              'field' =&gt; 'newPassword',
              'label' =&gt; 'Password',
              'rules' =&gt; 'required|matches[confirmPassword]'
          ),
          array(
              'field' =&gt; 'confirmPassword',
              'label' =&gt; 'Confirm Password',
              'rules' =&gt; 'required'
          )
      );
  
      $this-&gt;form_validation-&gt;set_rules($validate_data);
      $this-&gt;form_validation-&gt;set_error_delimiters('&lt;p class="text-danger"&gt;','&lt;/p&gt;');     
  
      if($this-&gt;form_validation-&gt;run() === true) {    
          $update = $this-&gt;model_users-&gt;changePassword($userId);                  
          if($update === true) {
              $validator['success'] = true;
              $validator['messages'] = "Successfully Update";
          }
          else {
              $validator['success'] = false;
              $validator['messages'] = "Error while inserting the information into the database";
          }           
      }   
      else {
          $validator['success'] = false;
          foreach ($_POST as $key =&gt; $value) {
              $validator['messages'][$key] = form_error($key);
          }           
      } // /else
  
      echo json_encode($validator);
  }
  
  public function validate_current_password()
  {
      $this-&gt;load-&gt;library('session');
      $userId = $this-&gt;session-&gt;userdata('id');
      $validate = $this-&gt;model_users-&gt;validate_current_password($this-&gt;input-&gt;post('currentPassword'), $userId);
  
      if($validate === true) {
          return true;
      } 
      else {
          $this-&gt;form_validation-&gt;set_message('validate_current_password', 'The {field} is incorrect');
          return false;           
      } // /else  
  }
  
  }
  </code></pre>
  
  <p>This is <strong>login.php</strong> in views folder</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;&lt;?php echo $title; ?&gt;&lt;/title&gt;
  
  &lt;!-- bootstrap css --&gt;
  &lt;link rel="stylesheet" type="text/css" 
  href="assets/bootstrap/css/bootstrap.min.css"&gt;
  &lt;!-- boostrap theme --&gt;
  &lt;link rel="stylesheet" type="text/css" href="assets/bootstrap/css/bootstrap-theme.min.css"&gt;
  
  &lt;!-- custom css --&gt;
  &lt;link rel="stylesheet" type="text/css" href="custom/css/custom.css"&gt;    
  
  &lt;!-- jquery --&gt;
  &lt;script type="text/javascript" src="assets/jquery/jquery.min.js"&gt;&lt;/script&gt;
  &lt;!-- boostrap js --&gt;
  &lt;script type="text/javascript" src="assets/bootstrap/js/bootstrap.min.js"&gt;
  &lt;/script&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
  
  
  &lt;div class="col-md-6 col-md-offset-3 vertical-off-4"&gt;
  &lt;div class="panel panel-default login-form"&gt;
      &lt;div class="panel-body"&gt;
          &lt;form method="post" action="index.php/users/login" id="loginForm"&gt;
              &lt;fieldset&gt;
                  &lt;legend&gt;
                      Login
                  &lt;/legend&gt;
  
                  &lt;div id="message"&gt;&lt;/div&gt;
  
                  &lt;div class="form-group"&gt;
                      &lt;label for="username"&gt;Username&lt;/label&gt;
                      &lt;input type="text" class="form-control" id="username" name="username" placeholder="Username" autofocus&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      &lt;label for="password"&gt;Password&lt;/label&gt;
                      &lt;input type="password" class="form-control" id="password" name="password" placeholder="Password"&gt;
                  &lt;/div&gt;                                           
  
                  &lt;button type="submit" class="col-md-12 btn btn-primary login-button"&gt;Submit&lt;/button&gt;                    
              &lt;/fieldset&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;script type="text/javascript" src="custom/js/login.js"&gt;&lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>login.js</strong></p>
  
  <pre><code>$(document).ready(function(){
  $("#loginForm").unbind('submit').bind('submit', function() {
  
      var form = $(this);
      var url = form.attr('action');
      var type = form.attr('method');
  
      $.ajax({
          url  : url,
          type : type,
          data : form.serialize(),
          dataType: 'json',
          success:function(response) {
              if(response.success === true) {
                  window.location = response.messages;
              }
              else {
                  if(response.messages instanceof Object) {
                      $("#message").html('');     
  
                      $.each(response.messages, function(index, value) {
                          var key = $("#" + index);
  
                          key.closest('.form-group')
                          .removeClass('has-error')
                          .removeClass('has-success')
                          .addClass(value.length &gt; 0 ? 'has-error' : 'has-success')
                          .find('.text-danger').remove();                         
  
                          key.after(value);
  
                      });
                  } 
                  else {                      
                      $(".text-danger").remove();
                      $(".form-group").removeClass('has-error').removeClass('has-success');
  
                      $("#message").html('&lt;div class="alert alert-warning alert-dismissible" role="alert"&gt;'+
                        '&lt;button type="button" class="close" data-dismiss="alert" aria-label="Close"&gt;&lt;span aria-hidden="true"&gt;&amp;times;&lt;/span&gt;&lt;/button&gt;'+
                        response.messages + 
                      '&lt;/div&gt;');
                  } // /else
              } // /else
          } // /if
      });
  
      return false;
  });
  });
  </code></pre>