Welcome Guest, Not a member yet? Register   Sign In
Best practice for saving URL before authenticating
#1

[eluser]ericbae[/eluser]
Here is the basic flow I want to achieve in my app.

1. user goes to a specific URL ABCD
2. since the URL requires user's authentication, it takes user to a login page and saves the url ABCD in session somewhere.
3. user logs in, then the site takes user back to url ABCD, which is what they originally wanted.

Now, what is the best practice for achieving this? (If there is any). I think logging in and then checking whether there is a URL in the session or not is a simple check that needs to be done on my authentication library (tank_auth for that matter).

But how about "saving" the url? In each controller methods, do I have to do

if( user_is_logged_in )
{
load->view('abcd');
}
else
{
save_to_session('abcd')
redirect "auth/login"
}

??

Any ideas?
#2

[eluser]toopay[/eluser]
you can do it without saving in session, by HTTP_REFERER.
#3

[eluser]guidorossi[/eluser]
I think toopay is right, you don't need to save the URL in a session, but you will need to save in a flash session something like "redirected = TRUE", so you know that the user came from a page that he can't accessa and need to be redirected, and not from the login page directly.
#4

[eluser]danmontgomery[/eluser]
I do:

Code:
if(!$this->auth->logged_in()) {
    $this->session->set_userdata('redir', current_url());
    redirect('login');
}

Then, when logging in:

Code:
if($redir = $this->session->userdata('redir')) {
    $this->session->unset_userdata('redir');
}

redirect($redir ? $redir : BASE_URL);

The problem with using flashdata or http_referer is that the redirect is lost on a failed login attempt unless you add some logic to keep it, which can get confusing.
#5

[eluser]ericbae[/eluser]
hey @noctrum.

Exactly what I did. Smile Thanks for sharing.




Theme © iAndrew 2016 - Forum software by © MyBB