I'm trying to implement logout in Codeigniter. Here is what I'm doing:
public function logout() {
if($this->session->userdata('session_id')){
$this->session->unset_userdata('logged_in');
$this->session->unset_userdata('session_id');
$this->session->unset_userdata('email');
$this->session->sess_destroy();
redirect('welcome', 'refresh');
}
}
And here is the login function:
public function authentication() {
$this->load->helper('html');
$this->load->library('javascript');
$this->load->helper('url');
$this->load->library('session');
$this->load->model('Tecnici_Model');
$this->load->library('encrypt');
$this->load->library('form_validation');
$this->form_validation->set_rules('email','Email','required');
$this->form_validation->set_rules('password','Password','required');
$email = $this->input->post('email');
$password = md5($this->input->post('password'));
$email = mysql_real_escape_string($email);
$password = mysql_real_escape_string($password);
$this->db->where('e_mail', $email, TRUE);
$this->db->where('password', $password, TRUE);
$query = $this->db->get("tecnici");
$result = $query->num_rows();
if($result > 0) {
$newdata = array(
'session_id' => md5(uniqid(rand(), true)),
'email' => $email,
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
redirect('welcome');
} else {
redirect('tecnici/login');
}
}
The authentication works correctly because the session_id is set, but when I click the logout button it stops in the logout method by showing a blank page. Where am I doing wrong? Thanks!
UPDATE 1
Here is the index() function of Welcome controller:
public function index() {
$this->load->helper('html');
$this->load->library('javascript');
$this->load->helper('url');
$this->load->library('session');
$this->load->view('header');
$this->load->view('welcome_message');
}
UPDATE 2
I've tried @AdrienXL solution:
public function user_logout() {
$this->session->sess_destroy();
redirect('welcome');
}
and I have printed the session id on the 'welcome' view. When I hit the logout button the session id change everytime. I can't explain why..