The following snippet is returning an error, however the error goes away when there is no SESSION.
Here is the line that is referred to by the error:
$user_id = $this->session->userdata('user_id');
Here is the class definition that the previous line is contained within:
class Dashboard extends CI_Controller
{
public function _construct()
{
parent::_construct();
$user_id = $this->session->userdata('user_id');
if(!user_id){
$this->logout();
}
}
public function index()
{
$this->load->view('dashboard/inc/header_view');
$this->load->view('dashboard/dashboard_view');
$this->load->view('dashboard/inc/footer_view');
}
public function logout()
{
$this->sess_destroy();
redirect('/');
}
}
What am I doing wrong?
On your log out function
Where $this->sess_destroy();
is
Replace with $this->session->sess_destroy();
because you are missing the session
Make sure $this->load->library('session');
is loaded.
public function _construct() {
parent::_construct();
$this->load->library('session'); // Or Autoload it.
if ($this->session->userdata('user_id') == FALSE) {
$this->logout();
}
}