Welcome Guest, Not a member yet? Register   Sign In
destroy session then set flash data
#1

[eluser]bigtimslim[/eluser]
I'm playing around with an auth library and have been using flash data for messages like 'you have logged in', etc.

Now I get to the logout function and I want to set flash data saying the user has successfully logged out after the session is destroyed. When I test this, all of the session data is preserved. controller just looks life this:
[/code]
function logout()
{
$this->auth->logout();
$this->session->set_flashdata('msg', 'You have logged out.');
redirect('home');
}
[code]


I noticed that the user guide says sess_destroy shouuld be the last function called. Any ideas?
#2

[eluser]NZmuzzer[/eluser]
I have a very similar problem. Using v1.7 session class with database. When the user logs out the session record (database row) is removed and a new one created for the now logged out (guest) user. A flashdata messeage is then set, "You are now logged out", and the page redirected to the homepage.

Code:
function logout()
    {
        $this->wd_auth->logout();
        $this->session->set_flashdata( 'notice', '<p>Welcome, you are now logged out</p>' );
        redirect( 'homepage' );
    }

and the wd_auth->logout() function in my auth library:
Code:
function logout()
    {
        $this->ci =& get_instance();
        $config =& get_config();
        
        // delete user record from session table
        $this->ci->db->where( 'session_id', $this->ci->session->userdata( 'session_id' ) );
        if( $query = $this->ci->db->delete( $config['sess_table_name'] ))
            return FALSE;
        
        // destroy the session
        $this->CI->session->sess_destroy();
        return TRUE;
    }
However, the flashdata (which should be associated with the new session record) is not carried over to the next page. Where's it gone??
#3

[eluser]thinkigniter[/eluser]
[quote author="NZmuzzer" date="1229220857"]I have a very similar problem. Using v1.7 session class with database. When the user logs out the session record (database row) is removed and a new one created for the now logged out (guest) user. A flashdata messeage is then set, "You are now logged out", and the page redirected to the homepage.

Code:
function logout()
    {
        $this->wd_auth->logout();
        $this->session->set_flashdata( 'notice', '<p>Welcome, you are now logged out</p>' );
        redirect( 'homepage' );
    }

[/quote]
Try changing function logout() to this...
Code:
function logout()
    {
        $this->ci =& get_instance();
        $config =& get_config();
        
        // delete user record from session table
        $this->ci->db->where( 'session_id', $this->ci->session->userdata( 'session_id' ) );
        if( $query = $this->ci->db->delete( $config['sess_table_name'] ))
            return FALSE;
        
        // destroy the session
        $this->CI->session->sess_destroy();
     $this->session->set_flashdata( 'notice', '<p>Welcome, you are now logged out</p>'
        return TRUE;
    }
#4

[eluser]NZmuzzer[/eluser]
thinkigniter, I'm not sure I see the difference, am I missing something obvious? Can you please explain how altering as you suggests fixes the flashdata issue.
Thx
#5

[eluser]Warz[/eluser]
Bump...!

I also got this issue... This won't work, cannot display flash data on pages/home. It has to do with sess_destroy(). Is there any good solution to this issue?

Code:
function logout()
    {
        $this->mx_auth->is_logged_in();
  
        $this->session->sess_destroy();  
        $this->message_model->success('You have been successfully logged out.'); // Sets Flash data
        redirect('pages/home');
    }
#6

[eluser]WanWizard[/eluser]
Think that's pretty obvious. You just destroyed the session, how should CI save session variables after the session has been destroyed?

In general, if you have organised your use of session variables well, there is no need to destroy the session. Just removing your authentication variables should be enough to force a logout. This also allows you to retain other session information. If you insist on destroying the session and you want (in the same page request) to use sessions again, first create a new session:
Code:
$this->session->sess_create();
#7

[eluser]Warz[/eluser]
Thank you! I thought the flash data worked with the sessions and created one, anyways you're right shouldn't need to delete it I just went with
Code:
$this->session->set_userdata('is_logged_in',FALSE);




Theme © iAndrew 2016 - Forum software by © MyBB