title: Codeigniter login system with session to redirect user to page if password correct
body: <p>I created a login system but every time I setup an if statement it loops back to the login page when I enter correct password. I need the index function in the controller, the list_employee function and View_employee function to redirect user to login page if they access it directly but if they enter correct password allow them to go to it.</p>
  
  <p>user_authentication controller</p>
  
  <pre><code>&lt;?php
  
  session_start(); //we need to start session in order to access it through CI
  
  Class User_Authentication extends CI_Controller {
  
  public function __construct() {
  parent::__construct();
  
  // Load form helper library
  $this-&gt;load-&gt;helper('form');
  
  // Load form validation library
  $this-&gt;load-&gt;library('form_validation');
  
  // Load session library
  $this-&gt;load-&gt;library('session');
  
  // Load database
  $this-&gt;load-&gt;model('login_database');
  
  }
  
  // Show login page
  public function user_login_show() {
  $this-&gt;load-&gt;view('login_form');
  }
  
  // Show registration page
  public function user_registration_show() {
  $this-&gt;load-&gt;view('registration_form');
  }
  
  // Validate and store registration data in database
  public function new_user_registration() {
  
  // Check validation for user input in SignUp form
  $this-&gt;form_validation-&gt;set_rules('name', 'Name', 'trim|required|xss_clean');
  $this-&gt;form_validation-&gt;set_rules('username', 'Username', 'trim|required|xss_clean');
  $this-&gt;form_validation-&gt;set_rules('email_value', 'Email', 'trim|required|xss_clean');
  $this-&gt;form_validation-&gt;set_rules('password', 'Password', 'trim|required|xss_clean');
  if ($this-&gt;form_validation-&gt;run() == FALSE) {
  $this-&gt;load-&gt;view('registration_form');
  } else {
  $data = array(
  'name' =&gt; $this-&gt;input-&gt;post('name'),
  'user_name' =&gt; $this-&gt;input-&gt;post('username'),
  'user_email' =&gt; $this-&gt;input-&gt;post('email_value'),
  'user_password' =&gt; $this-&gt;input-&gt;post('password')
  );
  $result = $this-&gt;login_database-&gt;registration_insert($data) ;
  if ($result == TRUE) {
  $data['message_display'] = 'Registration Successfully !';
  $this-&gt;load-&gt;view('login_form', $data);
  } else {
  $data['message_display'] = 'Username already exist!';
  $this-&gt;load-&gt;view('registration_form', $data);
  }
  }
  }
  
  // Check for user login process
  public function user_login_process() {
  
  $this-&gt;form_validation-&gt;set_rules('username', 'Username', 'trim|required|xss_clean');
  $this-&gt;form_validation-&gt;set_rules('password', 'Password', 'trim|required|xss_clean');
  
  if ($this-&gt;form_validation-&gt;run() == FALSE) {
  $this-&gt;load-&gt;view('login_form');
  } else {
  $data = array(
  'username' =&gt; $this-&gt;input-&gt;post('username'),
  'password' =&gt; $this-&gt;input-&gt;post('password')
  );
  $result = $this-&gt;login_database-&gt;login($data);
  if($result == TRUE){
  $sess_array = array(
  'username' =&gt; $this-&gt;input-&gt;post('username')
  );
  
  // Add user data in session
  $this-&gt;session-&gt;set_userdata('logged_in', $sess_array);
  $result = $this-&gt;login_database-&gt;read_user_information($sess_array);
  if($result != false){
  $data = array(
  'name' =&gt;$result[0]-&gt;name,
  'username' =&gt;$result[0]-&gt;user_name,
  'email' =&gt;$result[0]-&gt;user_email,
  'password' =&gt;$result[0]-&gt;user_password
  );
  redirect('employee');
  }
  }else{
  $data = array(
  'error_message' =&gt; 'Invalid Username or Password'
  );
  $this-&gt;load-&gt;view('login_form', $data);
  }
  }
  }
  
  // Logout from admin page
  public function logout() {
  
  // Removing session data
  $sess_array = array(
  'username' =&gt; ''
  );
  $this-&gt;session-&gt;unset_userdata('logged_in', $sess_array);
  $data['message_display'] = 'Successfully Logout';
  $this-&gt;load-&gt;view('login_form', $data);
  }
  }
  
  ?&gt;
  </code></pre>
  
  <p>employee controller</p>
  
  <pre><code>&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  
  class Employee extends CI_Controller {
  
      function __construct()
      {
          parent::__construct();
          $this-&gt;load-&gt;model('login/employee_model');
  
          }   
  
      //Shows the dashboard
      public function index()
      {
  
          $this-&gt;load-&gt;view('header');
          $this-&gt;load-&gt;view('employee');
          $this-&gt;load-&gt;view('login/footer');
  
  
  
      }
      //Insert the employee 
      public function  insert_employee()
      { 
  
  
          $data=array('name'=&gt;$this-&gt;input-&gt;post('name'),
              'LanId'=&gt;$this-&gt;input-&gt;post('LanId'),
              'reason'=&gt;$this-&gt;input-&gt;post('reason'),
              'PepNumber'=&gt;$this-&gt;input-&gt;post('PepNumber'),
              'Employee_Number'=&gt;$this-&gt;input-&gt;post('Employee_Number'),
              'department'=&gt;$this-&gt;input-&gt;post('department'),
  
              'status'=&gt;1);
          //print_r($data);
  
          $result=$this-&gt;employee_model-&gt;insert_employee($data);
          if($result==true)
          {
              $this-&gt;session-&gt;set_flashdata('msg',"Employee Records Added Successfully");
              redirect('employee');
  
          }
          else
          {
  
              $this-&gt;session-&gt;set_flashdata('msg1',"Employee Records Added Failed");
              redirect('employee');
  
  
          }
      }
      //List of Employees 
          public function list_employees()
      {
  
  
  
              $data['employee']=$this-&gt;employee_model-&gt;get_employee();
              $this-&gt;load-&gt;view('header');
              $this-&gt;load-&gt;view('list_of_employees',$data);
               $this-&gt;load-&gt;view('login/footer');
  
      }
      //List of Employees 
          public function viewlist_employees()
      {
  
  
              $data['employee']=$this-&gt;employee_model-&gt;get_employee();
              $this-&gt;load-&gt;view('header');
              $this-&gt;load-&gt;view('viewlist_of_employees',$data);
               $this-&gt;load-&gt;view('login/footer');
  
      }
  
      public function delete_employee()
      {
          $id=$this-&gt;input-&gt;post('id');
          $data=array('status'=&gt;0);
          $result=$this-&gt;employee_model-&gt;delete_employee($id,$data);
          if($result==true)
          {
              $this-&gt;session-&gt;set_flashdata('msg1',"Deleted Successfully");
              redirect('employee/list_employees');
  
          }
          else
          {
  
              $this-&gt;session-&gt;set_flashdata('msg1',"Employee Records Deletion Failed");
              redirect('employee/list_employees');
  
  
          }
  
      }
      public function edit_employee()
      {
          $id=$this-&gt;uri-&gt;segment(3);
          $data['employee']=$this-&gt;employee_model-&gt;edit_employee($id);
          $this-&gt;load-&gt;view('header',$data);
          $this-&gt;load-&gt;view('edit_employee');
      }
      public function  update_employee()
      {
          $id=$this-&gt;input-&gt;post('id');
  
          $data=array('name'=&gt;$this-&gt;input-&gt;post('name'),
              'LanID'=&gt;$this-&gt;input-&gt;post('LanID'),
              'reason'=&gt;$this-&gt;input-&gt;post('reason'),
              'PepNumber'=&gt;$this-&gt;input-&gt;post('PepNumber'),
              'Employee_Number'=&gt;$this-&gt;input-&gt;post('Employee_Number'),
              'department'=&gt;$this-&gt;input-&gt;post('department'),
  
              'status'=&gt;1);
  
          $result=$this-&gt;employee_model-&gt;update_employee($data,$id);
          if($result==true)
          {
              $this-&gt;session-&gt;set_flashdata('msg',"Employee Records Updated Successfully");
              redirect('employee/list_employees');
  
          }
          else
          {
  
              $this-&gt;session-&gt;set_flashdata('msg1',"No changes Made in Employee Records");
              redirect('employee/list_employees');
  
  
          }
      }
  
  }
  ?&gt;
  </code></pre>
  
  <p>login_database model</p>
  
  <pre><code>&lt;?php
  
  Class Login_Database extends CI_Model {
  
  // Insert registration data in database
  public function registration_insert($data) {
  
  // Query to check whether username already exist or not
  $condition = "user_name =" . "'" . $data['user_name'] . "'";
  $this-&gt;db-&gt;select('*');
  $this-&gt;db-&gt;from('user_login');
  $this-&gt;db-&gt;where($condition);
  $this-&gt;db-&gt;limit(1);
  $query = $this-&gt;db-&gt;get();
  if ($query-&gt;num_rows() == 0) {
  
  // Query to insert data in database
  $this-&gt;db-&gt;insert('user_login', $data);
  if ($this-&gt;db-&gt;affected_rows() &gt; 0) {
  return true;
  }
  } else {
  return false;
  }
  }
  
  // Read data using username and password
  public function login($data) {
  
  $condition = "user_name =" . "'" . $data['username'] . "' AND " . "user_password =" . "'" . $data['password'] . "'";
  $this-&gt;db-&gt;select('*');
  $this-&gt;db-&gt;from('user_login');
  $this-&gt;db-&gt;where($condition);
  $this-&gt;db-&gt;limit(1);
  $query = $this-&gt;db-&gt;get();
  
  if ($query-&gt;num_rows() == 1) {
  return true;
  } else {
  return false;
  }
  }
  
  // Read data from database to show data in admin page
  public function read_user_information($sess_array) {
  
  $condition = "user_name =" . "'" . $sess_array['username'] . "'";
  $this-&gt;db-&gt;select('*');
  $this-&gt;db-&gt;from('user_login');
  $this-&gt;db-&gt;where($condition);
  $this-&gt;db-&gt;limit(1);
  $query = $this-&gt;db-&gt;get();
  
  if ($query-&gt;num_rows() == 1) {
  return $query-&gt;result();
  } else {
  return false;
  }
  }
  
  }
  
  ?&gt;
  </code></pre>
  
  <p>employee_model</p>
  
  <pre><code>&lt;?php
  
  class Employee_model extends CI_Model 
  {
  
      public function insert_employee($data)
      {
          $this-&gt;db-&gt;insert('employee_list',$data);
          return ($this-&gt;db-&gt;affected_rows() != 1 ) ? false:true;
      }
      public function get_employee()
      {
          $this-&gt;db-&gt;select('*');
          $this-&gt;db-&gt;from('employee_list');
          $this-&gt;db-&gt;where('status',1);
  
          $query =$this-&gt;db-&gt;get();
          return $query-&gt;result();
      }
      public function delete_employee($id,$data)
      {
          $this-&gt;db-&gt;where('id',$id);
          $this-&gt;db-&gt;update('employee_list',$data);
          return ($this-&gt;db-&gt;affected_rows() != 1 ) ? false:true;
      }
      public function edit_employee($id)
      {
          $this-&gt;db-&gt;select('*');
          $this-&gt;db-&gt;from('employee_list');
          $this-&gt;db-&gt;where('id',$id);
          $this-&gt;db-&gt;where('status',1);
          $query =$this-&gt;db-&gt;get();
          return $query-&gt;result();
  
      }
      public function update_employee($data,$id)
      {
          $this-&gt;db-&gt;where('id',$id);
          $this-&gt;db-&gt;update('employee_list',$data);
          return ($this-&gt;db-&gt;affected_rows() != 1 ) ? false:true;
      }
  }
  </code></pre>