Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter: Dynamic post-login redirection?

I'm building a basic CodeIgniter site that requires a login before you can access any of the site.

If a user visits some site url, something like this:

http://www.mysite.com/project/detail/2049

AND they are current logged out, I have it set to automatically kick them back to the login page.

My question is, after they login, what is the best way to redirect them to the original URL they typed in, instead of say, redirecting them to the websites homepage?

I was thinking maybe, dynamically create the URL as a hidden form element in the login form and redirect there upon a successful login... What do you guys think? Is there a better/best practice for this type of dynamic post-login redirection?

like image 611
Jake Wilson Avatar asked Jan 25 '10 05:01

Jake Wilson


1 Answers

When they hit the restricted page record the uri and set it as session data with

this->session->set_userdata('redirect', 'page/uri/here');

then redirect them to the login / register

after they login check to see if 'redirect' is present with

if($this->session->userdata('redirect'))
{
    redirect($this->session->userdata('redirect'));
}

if it doesn't then take them wherever you normally take them after a login

like image 122
Tom Schlick Avatar answered Sep 18 '22 17:09

Tom Schlick