Reputation: 3
I'm getting a undefined index error when checking if a session exists. The error only occurs when a session does not exist.
Controller where the session is created:
$data = array(
'email' => $this->input->post('email'),
'is_logged_in' => TRUE
);
$this->session->set_userdata($data);
redirect('site/index');
Controller where the check takes place:
$is_logged_in = $this->session->userdata['is_logged_in']; // error occurs here
if(!isset($is_logged_in) || $is_logged_in !== TRUE)
{
$data['main_content'] = 'login_form';
$this->load->view('includes/template', $data);
}
I've made sure the session library is loaded.
Upvotes: 0
Views: 1219
Reputation: 2489
You are treating a method as an associative array. In codeigniter userdata
is a function which returns the value of the index you pass it. So your line:
$is_logged_in = $this->session->userdata['is_logged_in'];
Should be:
$is_logged_in = $this->session->userdata('is_logged_in');
Upvotes: 3
Reputation: 4821
$this->session->userdata['is_logged_in'];
Should be
$this->session->userdata('is_logged_in');
The difference is ()
instead of []
because userdata is a function, not an array.
Per the documentation
Upvotes: 1