CodeIgniter: dynamic redirection after login?

I am creating a basic CodeIgniter site that requires a login before you can access any site.

If the user visits the site URL, something like this:

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

And they are currently logged out, I set them to automatically turn off back to the login page.

My question is: after they logged in, what is the best way to redirect them to the source URL they typed in, instead of redirecting them to the websites homepage?

I thought maybe dynamically creating the URL as a hidden form element in the login form and redirecting there after a successful login ... What do you guys think? Is there any best / best practice for this type of dynamic redirection after login?

+5
source share
4 answers

When they click on a restricted page, write the uri and set it as session data using

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

then redirect them to login / register

after they log in to check if 'redirect' is present with

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

if he doesn’t accept them, wherever you usually accept them after logging in

+16
source

when the access attempt is intercepted:

redirect('/public/login/r'.$this->uri->uri_string());

so in your case, after redirecting, the url might look like this:

http://www.example.com/public/login/r/project/detail/2049

if login is successful

$uri = $this->uri->uri_string();
$redirect = substr($uri, strpos($uri, '/r/')+2);
redirect($redirect);

will be redirected to the original resource.

(and no, +2 should not be +3)

0
source

, , ?

Create it in the library so you can call the following:

$this->mylibrary->login($user);

and

$this->mylibrary->is_logged_in($user); on top of each page and automatically redirect visitors to your main site.

0
source

I am using flashdata for redirection.

this->session->set_flashdata('redirect_url', 'page/uri/here');

after logging in to check if 'redirect_url' is present with

if($this->session->flashdata('redirect_url'))
{
    redirect(base_url().$this->session->flashdata('redirect_url')));
}

Hope for this help

0
source

All Articles