'How to use $this->session->set_userdata in codeigniter

I have some promblem with CI when i use $this->session->set_userdata

Controller

 public function check_login()
{
    $email    = $this->input->post('username_email');
    $password = $this->input->post('passwords');
    $password_encrypt = sha1(md5($password));

    $query = $this->model->check_login($email,$password_encrypt);
    if( $query->num_rows() > 0 )
    {
        $row = $query->row(1);
        $data = array(
          'email'           => $row->email,
          'level_id'        => $row->level_id,
          'id_subunit'      => $row->id_subunit,
        );
        $this->session->set_userdata($data);
        return TRUE;
    }
    else{
        return FALSE;
    }
}

Model

function check_login($email,$password_encrypt)
{
    $db = $this->load->database('default',TRUE);
    $where_email = array('email' => $email, 'password' => $password_encrypt);

    $db->where($where_email);
    $query = $db->get('admin',1);

    return $query;
}

I try to get in here, to get a session data

public function home()
{
    $data['level'] = $this->session->userdata('level_id');
    $data['email'] = $this->session->userdata('email');
    $data['id_subunit'] = $this->session->userdata('id_subunit');

    if($data['level'] == 1){
        redirect('admin');
    }
    else if($data['level'] == 2){
        redirect('kuesioner/submited'); 
    }
    /*else{
         redirect('sub_unit/home');
    }*/
}

My problem is i just can get email and level id from array in session

How to solved that?



Solution 1:[1]

Load session library in your controller or in config/autoload.php file.

$this->load->library('session');

then set the session data.

$this->session->set_userdata($data);

Solution 2:[2]

Maybe comma

$data = array(
  'email'           => $row->email,
  'level_id'        => $row->level_id,
  'id_subunit'      => $row->id_subunit
);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 disha
Solution 2 antelove