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 :)

 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.