CodeIgniter Forums
Session->destroy doesn't work when using Flash-sessions. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Session->destroy doesn't work when using Flash-sessions. (/showthread.php?tid=21630)



Session->destroy doesn't work when using Flash-sessions. - El Forum - 08-16-2009

[eluser]Slowcheetah[/eluser]
Why ALL the sessions stay alive when i execute the following code?

ALL SESSIONS STAY ALIVE??
Code:
function signout() {
        // destroy all sessions (username and password)
        $this->session->sess_destroy();
        // set user message
        $this->session->set_flashdata('msg', 'Succesfully signed out.');
        // Redirect to signin page
        redirect('');
    }

If i don't set the flash message the code works perfect, and all sessions get destroyed. But cannot report to user

ALL SESSIONS DESTROY SUCCESFULLY
Code:
function signout() {
        // destroy all sessions (username and password)
        $this->session->sess_destroy();
        // Redirect to signin page
        redirect('');
    }



Session->destroy doesn't work when using Flash-sessions. - El Forum - 08-16-2009

[eluser]wabu[/eluser]
I think what's happening is that destroy sets an empty cookie but you're resetting it before it's sent (empty) along with the headers. That's why sess_destroy() needs to be called last.

Instead of trying to blow everything away, how about selectively clearing your user session data with unset_userdata()?


Session->destroy doesn't work when using Flash-sessions. - El Forum - 08-16-2009

[eluser]Slowcheetah[/eluser]
Thanks Wabu, works like a charm now.

The only problem now is that i have to remember to unset all the sessions manually when i sign-out. Is it possible to loop trough all session items and unset them??

CURRENT CODE
Code:
function signout() {
        // destroy all sessions
        $account_data = array(
            'account_id'=>'', 'username'=>'', 'password'=>'', 'email'=>''
        );
        $this->session->set_userdata($account_data);
        // set user message
        $this->session->set_flashdata('msg', 'Succesfully signed out.');
        // Redirect to signin page
        redirect('');
    }