Welcome Guest, Not a member yet? Register   Sign In
Remember Last Page Visited... How?
#1

[eluser]dhall[/eluser]
I am new to website dev so sorry if this is a basic one.
I am building a member site that has a page that can be viewed by the public with limited functions.
If a user goes to that page not logged in, then clicks on the LogIn, redirects them to the LogIn page, once they are logged in I want to take them back to the page they were just on.

How do I do that? I am sure it has some fancy name, but I don't know it.
Thanks for the help!
#2

[eluser]term25[/eluser]
You can save the last url before redirect to login page using database.

So, what would you do is basically save every IP in a table (before you can make a check if it already exists, then you rewrite it) with the last URL before entering Login area.

After successfuly logging in, you can redirect to the last recorded URL.

In my opinion this is the best approach.

There are some libraries for CI that can remember e.g. last 3 urls etc, but the problem with them is that for example if you have 5 steps(pages) login , they are useless.

So, the best technique I know about is to save all urls the visitor (their IP address) visits before entering e.g. http://www.example.com/login

After entering (in this controller "login") http://www.example.com/login you stop recording/saving their url.

You can make an if condition for directly entering login page to redirect the user to homepage etc.

You can e.g. do not log/save logged in users to save some performance of your server etc.

Hope it somehow move you in the right direction.
#3

[eluser]dhall[/eluser]
I never thought about saving it in the database... interesting...

What are the CI Built-Ins that can track this?

I don't think I really need more than 1 page to remember.

Thanks for the thoughts!
#4

[eluser]rwestergren[/eluser]
Another way to handle it is to pass the redirect url to the login page as a parameter, and use it to redirect to after successful login. Something like this:

Code:
if(!$this->logged_in())
{
    // Build redirect parameter
    $redirect_param = urlencode(site_url('current/url/here'));
    redirect("login/$redirect_param");
}

You now have the url to redirect to after a successful login.
#5

[eluser]InsiteFX[/eluser]
current_url()

Returns the full URL (including segments) of the page being currently viewed.

Code:
$this->set_userdata('redirect', urlencode(current_url());
redirect($this->session->userdata('redirect'));
#6

[eluser]dhall[/eluser]
Very Cool.
I figured it would be something simple.
Thanks!
#7

[eluser]dhall[/eluser]
I tried implementing the above yet seem to have an issue with my code.

Here is my code inside my login function:

Code:
if (isset($this->session->userdata('redirect')))
{
  redirect($this->session->userdata('redirect'));
}
else
{
  redirect('home');
}

I get the following error:
Fatal error: Can't use method return value in write context.

What am I doing wrong?
#8

[eluser]PhilTem[/eluser]
An argument to isset must be a pointer to a variable (i.e. the variable name). In your case you are specifying just a string to isset, that's why it's complaining. Just remove the isset call and use

Code:
if ( $this->session->userdata('redirect') )

What I actually wanted to contribute: All these approaches are nice and cool, but what to do you do when a user opens some of your pages in multiple tabs? Trying to log in from the first tab will give a redirect to the last tab's url.
I don't know yet how to solve it with sessions or anything else but only to set a GET-parameter to the login-page with the former url. Or, with an inline form, make the current URL or URI a hidden field and redirect to that on successful login.
#9

[eluser]dhall[/eluser]
That did it... thanks!

Another question.

Where would be the best place to put the code where I set the 'redirect' variable in the session?

Code:
$this->set_userdata('redirect', urlencode(current_url());

Would I need to repeat this line of code in every controller; some controllers set it, other controllers clear it?




Theme © iAndrew 2016 - Forum software by © MyBB