Forum Moderators: coopster

Message Too Old, No Replies

Redirect to referring page after login

         

Brownie

4:13 pm on Aug 19, 2004 (gmt 0)

10+ Year Member



I am trying to redirect users to the referring page after they log in. So far, I can capture the referring URL but on submitting the login form, the referring URL changes to the current login page. I understand why the referring URL changes (since the form submits to itself), but I don't know how to go about fixing it! In the current code below, I have tried to send the referring URL to the sending the URL to the "userloginsuccess.php" page, but ideally I just want to redirect the user on successful user validation.

Even pointers as to what functions I need to perform would help... HELP!

<?php
session_start();
$url = getenv('HTTP_REFERER');
if ($url == "") { $url="http://www.myurl.com/";
session_register('url');
}

// *** Validate request to log in to this site.

$loginFormAction = $HTTP_SERVER_VARS['PHP_SELF'];
if (isset($accesscheck)) {
$accesscheck = $GLOBALS['PrevUrl'];
session_register('PrevUrl');
}

if (isset($HTTP_POST_VARS['UserEmail'])) {
$loginUsername=$HTTP_POST_VARS['UserEmail'];
$password=$HTTP_POST_VARS['UserPassword'];
$MM_fldUserAuthorization = "UserLevel";
$MM_redirectLoginSuccess = "userloginsuccess.php";
$MM_redirectLoginFailed = "userlogin.php?badlogin=true";
$MM_redirecttoReferrer = true;
mysql_select_db($database_visioneng, $visioneng);

$LoginRS__query=sprintf("SELECT UserEmail, UserPassword, UserLevel FROM usersweb WHERE UserEmail='%s' AND UserPassword='%s'",
get_magic_quotes_gpc()? $loginUsername : addslashes($loginUsername), get_magic_quotes_gpc()? $password : addslashes($password));

$LoginRS = mysql_query($LoginRS__query, $visioneng) or die(mysql_error());
$loginFoundUser = mysql_num_rows($LoginRS);
if ($loginFoundUser) {

$loginStrGroup = mysql_result($LoginRS,0,'UserLevel');

//declare two session variables and assign them
$GLOBALS['MM_Username'] = $loginUsername;
$GLOBALS['MM_UserGroup'] = $loginStrGroup;

//register the session variables
session_register("MM_Username");
session_register("MM_UserGroup");

if (isset($HTTP_SESSION_VARS['PrevUrl']) && true) {
$MM_redirectLoginSuccess = $HTTP_SESSION_VARS['PrevUrl'];
}
header("Location: ". $MM_redirectLoginSuccess . "?url=$url" );
}
else {
header("Location: ". $MM_redirectLoginFailed );
}
}
?>

jatar_k

4:40 pm on Aug 19, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what about writing the referer on first hit to the login page as a hidden field in your form and then using that to redirect.

Brownie

8:47 am on Aug 20, 2004 (gmt 0)

10+ Year Member



I have echoed to a hidden field, but because the form action calls the current page, the page reloads and picks up a new referring page (i.e. the userlogin.php page)...

jatar_k

3:55 pm on Aug 20, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



test if it is set and don't assign the value if it is already there.

if (isset($myreferer) &&!empty($myreferer)) {
// echo hidden field with out reassigning referer
} else {
$myreferer = $_SERVER['HTTP_REFERER'];
}