Page 1 of 1
redirect user to previous page after log in
Posted: Thu Jan 06, 2011 1:37 am
by jose1
Hello There,
I have a bit of a problem, i have a dynamic website and i want to restrict users to see content only after they've logged in, so my question is how do i redirect the user back to the content they were trying to see after they've logged in.
Any Help much apprecited.
Re: redirect user to previous page after log in
Posted: Thu Jan 06, 2011 2:45 am
by Phoenixheart
Basically you need to store the previous page's URI somewhere (often Session or GET).
After logging in, redirect the user with header() and clear the variable.
Re: redirect user to previous page after log in
Posted: Thu Jan 06, 2011 8:34 am
by idinx
You can use following code :
header ("location:$_SERVER[HTTP_REFERER]");
Re: redirect user to previous page after log in
Posted: Thu Jan 06, 2011 9:37 am
by Neilos
Code: Select all
function currentPage() {
$pageURL = 'http';
if($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
$currentPage = currentPage();
// run this script on the page that you want them to be redirected to after login.
// Save $currentPage to a session, GET or POST variable
Then on the login script use the header("location:" $thePage); function.
I in fact use this method for my login script and it works very well. I lifted this straight from my site, so it will work. I
borrowed it from a tutorial (modified a bit), so I have no qualms sharing it again lol.
'HTTP_REFERER'
The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.
This is not always the best option.
Re: redirect user to previous page after log in
Posted: Thu Jan 06, 2011 12:03 pm
by jose1
Thanks very much for the reply, my problem is that the user won't return to a specific page that i can send a header, the pages have different articles and some articles in some sections are restricted so i want the user to after log in be returned to the article they were trying to read regardless of the section. Hope that makes sense. Thanks for the help