Welcome Guest, Not a member yet? Register   Sign In
CI destroy session takes too long?
#1

[eluser]chaotiq[/eluser]
Sorry if this has been asked before.

I have controller with an index method that checks to see if the session exists. If it does then it goes to home.php . If not, then it sends the user to a login.php screen for credentials and then authenticates the user and sends them to the home.php screen. This part works.

Now I have a logout button on the home.php screen that calls the logout method in the same controller. The logout method is simple. It just destroys the session and calls the index function of the controller.
Code:
function logout()
    {
        $this->session->sess_destroy();
        
        $this->index();
    }//End of logout() function

The problem is that when I click the logout button it puts me right back to the home.php screen like the session was not destroyed. Once I click the logout button a second time I get taken back to the login.php screen like I wanted. Am loading the index method to 'quick'? so the session data is not destroyed on the first go around but is on the second?
#2

[eluser]chaotiq[/eluser]
I also read this thread (http://ellislab.com/forums/viewthread/135722/) a little and it states not to use CI's session class and use php's instead. Is that a good option? Will I loose out on a lot of functionality?
#3

[eluser]Ben Edmunds[/eluser]
The session isn't destroyed immediately. That is not due to CI.

Try redirecting to index after you destroy the session and that should solve your problem.
#4

[eluser]chaotiq[/eluser]
Do you mean check to see if the session is destroyed before I send it back to index?

Like so:
Code:
$this->session->sess_destroy();
        
while ($this->session->userdata('session_id')
{
        //Loop till session is destroyed.
}

$this->index();
#5

[eluser]n0xie[/eluser]
No he means you should redirect after you destroyed the session. The session will only be destroyed on the next request.
#6

[eluser]chaotiq[/eluser]
OK, I am sorry but I do not understand. I thought I was destroying the session by calling the sess_destroy() function. What should I do after I call the sess_destroy function? Leave them at the current page and then redirect them after a certain period of time? I do not believe I understand what is going on here.

Thank you for your help.
#7

[eluser]chaotiq[/eluser]
I think I got it. My index function would check to see if session data exists. If it did then it would send the user to the home screen. If it did not then it would send the user to the login screen. My logout function would destroy the session and then call the index function. Instead, I should destroy the session and redirect the user directly to the login screen bypassing the check in the index function to see if session data exists. Does this make sense?

Again, thank you for your help.
#8

[eluser]Michael Wales[/eluser]
Code:
$this->session->sess_destroy();
redirect('controller');
#9

[eluser]chaotiq[/eluser]
[quote author="Michael Wales" date="1264457299"]
Code:
$this->session->sess_destroy();
redirect('controller');
[/quote]


It is that simple. Wow. I think I have a problem making things way more complicated than they really are.

Thank you.
#10

[eluser]HdotNET[/eluser]
Code:
function sess_destroy()
    {
        // Kill the session DB row
        if ($this->sess_use_database === TRUE AND isset($this->userdata['session_id']))
        {
            $this->CI->db->where('session_id', $this->userdata['session_id']);
            $this->CI->db->delete($this->sess_table_name);
        }

        // Kill the cookie
        setcookie(
                    $this->sess_cookie_name,
                    addslashes(serialize(array())),
                    ($this->now - 31500000),
                    $this->cookie_path,
                    $this->cookie_domain,
                    0
                );
    }

Mr Wales is right... but to fully understand why you need to look at the sess_destroy method above in the session class.

All it does is set the cookie data to nothing, and deletes the db session data if required.

In the session class constructor there is this:

Code:
// Run the Session routine. If a session doesn't exist we'll
        // create a new one.  If it does, we'll update it.
        if ( ! $this->sess_read())
        {
            $this->sess_create();
        }
        else
        {
            $this->sess_update();
        }

Therefore you haven't killed it even when you call $this->session->sess_destroy();

The $this->session->userdata array is still there.

Its only really destroyed once another request is made to the server.




Theme © iAndrew 2016 - Forum software by © MyBB