Jump to content

Redirect to original page after login


TheEddy

Recommended Posts

You should echo the $_SERVER['HTTP_REFERER'] variable to see the exact content of the string, but after you process your form, your VERY FIRST HTTP output should come from code like the following:

 

header ('location:http://www.yoursite.com/'.$_SERVER['HTTP_REFERER']);

 

Depending on your site structure, this may work as well:

 

header ('location:your_folder/'.$_SERVER['HTTP_REFERER']);

 

NOTE: $_SERVER['HTTP_REFERER'] will not likely pass variables such as $_GET values, so you may need to manually build a code referral method and use that for the login. here is what I used recently on a site:

 

// sets referral page for login
if ($_GET['action'] != 'login') {
unset($_SESSION['refer']);
foreach ($_GET as $field=>$value) {
	$bld_refer .= '&'.$field.'='.strip_tags($value);
}
$_SESSION['refer'] = '?'.ltrim($bld_refer,'&');
}

//processes login
	switch ($get['task']) {
		case 'logout':
			$refer = $_SESSION['refer'];
			$new_user->logout_user();
			header('Location:'.$refer);
		break;
		case 'login':
			$vars = new variables($db);
			$req = $vars->req_fields();
			$entries = $new_user->usrLogin($_POST,$req['login']);
			if (isset($entries['error'])) { 
				$this_form->login_form($entries,$entries);
			} else {
				header('Location:'.$_SESSION['refer']);
			}
		break;
	}

 

I do have some classes that process the login, but if the method returns true, it kicks the user to the page the logged in from.

 

Hope that helps :)

 

It was better for me to use this:

function curPageURL() {
$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;
}
$r=curPageURL();
$_SESSION['REFERER']=$r;

 

Because I also needed to record the direct input from the browser.  Thanks for your help though!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.