sess_destroy() doesn't work properly

2019-04-15 22:17发布

问题:


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..

回答1:

The thing is, why do you test if the session is set or not if you are going to destroy it ? Also, sess_destroy() will destroy all the sessions, even flash ones. You don't need to unset them first.

A simple function should be enough :

public function logout()
{
    $this->session->sess_destroy();
    redirect('welcome');
}

http://www.codeigniter.com/user_guide/libraries/sessions.html

EDIT :

I think you are too focused on the session_id. Here is a quote from the CI doc :

When a page is loaded, the session class will check to see if valid session data exists in the user's session cookie. If sessions data does not exist a new session will be created and saved.

That's why you always have a session_id. In your case, you should check if there is a session named "logged_in" for example.