Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Destroy Session but keep flashdata

I am using Tank Auth for user management in my CI 1.7.3 App. Everything is working fine but I'm trying to set a flash_message to be displayed when the user logs out. The problem is the $this->tank_auth->logout(); function destroys the session. I have modified the logout function in the Tank Auth library to look like:

    function logout()   {
        $this->delete_autologin();

        // See http://codeigniter.com/forums/viewreply/662369/ as the reason for the next line
        $user_session_data = array('user_id' => '', 'username' => '', 'status' => '');
        $this->ci->session->set_userdata($user_session_data);
        $this->ci->session->unset_userdata($user_session_data);
    }

It was previously

function logout()
        {
            $this->delete_autologin();

            // See http://codeigniter.com/forums/viewreply/662369/ as the reason for the next line
            $this->ci->session->set_userdata(array('user_id' => '', 'username' => '', 'status' => ''));

            $this->ci->session->sess_destroy();
        }

In My controller I have

function logout(){
    if ($this->tank_auth->is_logged_in()) { // logged in
        $this->session->set_flashdata('status_message', $this->lang->line('auth_message_logged_out'));
        $this->tank_auth->logout();

        redirect('');           

    } 

}

If I remove the $this->tank_auth->logout(); function the message shows fine. I'm sure it's a simple session problem

like image 882
Brooke. Avatar asked Dec 27 '22 23:12

Brooke.


1 Answers

If you try to set flashdata while using a database in the same request after you call sess_destroy(), it won't work (because there is no session to append the flashdata to).

To fix this problem, add $this->ci->session->sess_create(); after the call to sess_destroy(). This works because you're re-creating the session before trying to append data to it. This is the only way to use flashdata after a sess_destroy() if you're using sessions in a database.

like image 131
Brendan Avatar answered Jan 07 '23 23:01

Brendan