Tachi
Tachi

Reputation: 2212

$this->session->unset_userdata not working?

So I have this login method:

public function login(){
    $this->form_validation->set_rules('username','Username','trim|required|min_length[4]|xss_clean');
    $this->form_validation->set_rules('password','Username','trim|required|min_length[4]|xss_clean');

    if($this->form_validation->run()== FALSE) {

        //Loading View
        $this->load->view('admin/layouts/login');


        $username = $this->input->post('username');
        $password = $this->input->post('password');

        //Validate Username & Password
        $user_id = $this->Authenticate_model->login($username, $password);

        if($user_id){
            $user_data = array(
                'user_id' => $user_id,
                'username' => $username,
                'logged_in' => true
            );

            //Set session userdata
            $this->session->set_userdata($user_data);
        } else {
            //Set message
            $this->session->set_flashdata('pass_login', 'You are now logged in');
            redirect('admin/dashboard');
        }
    }
}

And then i use this simple method to logout:

public function logout(){
    //Unset User Data
    $this->session->unset_userdata('user_id');
    $this->session->unset_userdata('username');
    $this->session->unset_userdata('logged_in');
    $this->session->sess_destroy();

    redirect('admin/authenticate/login');
}

So basically I'm unsetting all my sessions userdata and then redirecting back to login controller. And what happens is, when i redirect back to login page, I automatically login again, like if my session data was still valid and present. Why it's happening?

Upvotes: 0

Views: 6034

Answers (2)

Fs-Gen
Fs-Gen

Reputation: 1

maybe you place "redirect if session == true" code at __construct, there is my problem :v

Upvotes: -1

user4419336
user4419336

Reputation:

You could try

unset($this->session->userdata('user_id'));
unset($this->session->userdata('logged_in'));
unset($this->session->userdata('username'));

Or Just Have

$this->session->sess_destroy();

Make sure your session library auto loaded and have configured your settings depending on version of codeigniter

Upvotes: 3

Related Questions