Welcome Guest, Not a member yet? Register   Sign In
session unset_userdata not working before redirect
#1

[eluser]CI Nick[/eluser]
Hello all.

Here's the scenario:

User tries to access a secure page on my site. Sentry class checks if they are logged in and redirects them to the login page. I then store the url they were trying to access in the session (using standard CI session cookie). If they login successfully, they are sent to the page they were originally trying to access by retrieving the url from the cookie. I've written a library so that every redirect first checks if there is a url saved in the cookie before the redirect happens:

Code:
class Redirect {
    
    var $ci;
    
    function Redirect()
    {
        //Get CI instance
        $this->ci =& get_instance();
    }
    
    function send_to($url_default)
    {
        if($url_to = $this->ci->session->userdata('url_to'))
        {
            $this->ci->session->unset_userdata('url_to');
            return $url_to;
        }
        else
        {
            return $url_default;
        }
    }

}

And all my redirect calls look like this:

Code:
redirect($this->redirect->send_to('my_page'));

So if there is a saved url in the cookie, you get redirected to that page, otherwise, you get redirected to the page you send to the "send_to" method in my Redirect class.

The problem is that the unset_userdata call (when a stored url has been found) is not un-setting that session variable. If I put an exit call after the unset it works, so it's as if the code is not getting a chance to run before the return happens.

If anyone followed all that, would you know why this is hapening? Is it bad code from me or is there a bug with CI?

Thanks in advance,
Nick.
#2

[eluser]Michael Wales[/eluser]
I can't confirm, but I imagine it's because of the way you are redirecting. The default method of redirecting is via a header, which must come before anything else on the pages output (meaning it is coming before your session work). If you switch to the refresh method of redirecting (although messy) it should work properly.

Personally, I would look at refactoring the code a bit - maybe approach the problem in a different manner. I personally dislike the refresh method of redirecting...
#3

[eluser]CI Nick[/eluser]
Hi Michael,

You are absolutely right -- thank you so much!!

I notice that with the refresh method, you do get a noticeable refresh of the page. I may try and re-address the problem and see if I can come up with a more elegant solution.

Thanks again though... that was driving me mad!!

Nick.
#4

[eluser]adamp1[/eluser]
Why not look at flash variables in the session class. I have found them pretty useless, but for this thing they work great. I know its just a session cookie but you don't have to worry about unsetting it and such.
#5

[eluser]Michael Wales[/eluser]
I tend to use flash variables for the same thing adamp - I highly recommend them for this purpose.
#6

[eluser]CI Nick[/eluser]
Thanks for the suggestion guys. I'll give flash variables a shot.

Thanks,
Nick.




Theme © iAndrew 2016 - Forum software by © MyBB