redirect user to previous page after log in

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jose1
Forum Newbie
Posts: 4
Joined: Thu Jan 06, 2011 1:23 am

redirect user to previous page after log in

Post 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.
Phoenixheart
Forum Contributor
Posts: 123
Joined: Tue Nov 16, 2004 7:46 am
Contact:

Re: redirect user to previous page after log in

Post 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.
User avatar
idinx
Forum Newbie
Posts: 2
Joined: Wed Jan 05, 2011 12:29 am
Location: Iran - Tehran

Re: redirect user to previous page after log in

Post by idinx »

You can use following code :

header ("location:$_SERVER[HTTP_REFERER]");
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: redirect user to previous page after log in

Post 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.
jose1
Forum Newbie
Posts: 4
Joined: Thu Jan 06, 2011 1:23 am

Re: redirect user to previous page after log in

Post 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
Post Reply