Search code examples
javascriptphpcodeigniter-2php-5.3codeigniter-3

session not regestring the 3 variable


I have created a login system in codeigniter. It's working fine, but when i login it registers only 2 variable which are login and password but not showing me the 3rd variable in the session which is user_type. I have tried too much but i can not understand what is wrong.

model code

  function login($username, $password) {
    //create query to connect user login database
    $this->db->select('user_id, username, password','user_type');
    $this->db->from('login');
    $this->db->where('username', $username);
    $this->db->where('password', $password);
    $this->db->where('status',1);
    $this->db->limit(1);

    //get query and processing
 echo  $query = $this->db->get();
    if($query->num_rows() == 1) { 
        return $query->result(); //if data is true
    } else {
        return false; //if data is wrong
    }
}

varifying code controller

function index() {
$this->form_validation->set_rules('username', 'username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'password', 'trim|required|xss_clean|callback_check_database');

    if($this->form_validation->run() == FALSE) {
        $this->load->view('v_login');
        } else {
            //Go to private area
            redirect(base_url('c_home'), 'refresh');
        }
 }

 function check_database($password) {
     //Field validation succeeded.  Validate against database
     $username = $this->input->post('username');
     //query the database
     $result = $this->login->login($username, $password); 
     if($result) {
         $sess_array = array();
         foreach($result as $row) {
             //create the session
             $sess_array = array('user_id' => $row->user_id,
                 'username' => $row->username,
                 'user_type'=> $row->user_type
                  );
             //set session with value from database
             $this->session->set_userdata('logged_in', $sess_array);
             }
      return TRUE;
      } else {
          //if form validate false
  $this->form_validation->set_message('check_database', '<p style="color: red;">اسم المستخدم / كلمة المرور غير صحيحة ! </p>');
          return FALSE;
      }
  }

session variable code

function session_start(){
    /*----------------------------  session start here */

    if($this->session->userdata('logged_in'))
    {
        $session_data = $this->session->userdata('logged_in');
        $data['username'] = $session_data['username'];
        $data['id'] = $session_data['user_id'];
        $data['user_type'] = $session_data['user_type'];





    } else {
        //If no session, redirect to login page
        redirect('c_login', 'refresh');
    }
    /* ---------------------- session start ends */

Solution

  • Because you are not selecting user_type in your select query.

    Change this :

    $this->db->select('user_id, username, password','user_type');
    

    With this :

    $this->db->select('user_id, username, password,user_type');