Go back to last webpage after logging in

829 views Asked by At

I need help figuring out how to allow users who login go back to the page that they were on before being sent to the login page. Here is a quote from my boss:

Sometimes I forget to sign in and I go to a page, click and get the notice about joining or signing in. That is fine, but is there a way, once I sign in to open the page I was trying to open prior to signing in, instead of having to go through all the navigation again?

I'm using PHP to do this project.

1

There are 1 answers

0
David On

Whatever mechanism forwards the user to the sign-in page should include the original page's URL as a parameter. Something like:

header('Location: http://example.com/login.php?redirectTo=' + urlencode($_SERVER['REQUEST_URI']));

Then the login.php page can redirect to that page after the user authenticates:

header('Location: ' . $_GET['redirectTo']);

You may want to put in some checking on the redirectTo value in login.php to make sure nobody's trying to do anything malicious, I suppose. Though I can't currently think of anything malicious they could do. (Though you would want to include a default if no URL was provided.)

But the general idea is that the authentication mechanism provides the login page with a redirect URL when it detects that the user needs to login.