Page 1 of 1

[SOLVED] Redirect to previous page upon login...

Posted: Wed Jun 30, 2004 8:22 pm
by dardsemail
I have a login script (to which I'm forever indebted to those in this forum for helping me stumble my way through it!). In any event, I'd like to have the users be taken to the page in which they were trying to access upon successful login.

For example, if a user is trying to get to their customer details page, the page would trigger a login and then the login.php page would redirect them back to the customer details page upon successful login.

I have the following script in my login page, but I'm not 100% clear on how to redirect the user to the refering page:

Code: Select all

<?php
// *** Validate request to login to this site.
session_start();

if ($_POST['submit'])
{ 
	$frm_username = @$_POST['frm_username']; 
	$frm_password= @$_POST['frm_password']; 
	
	authenticateUser($frm_username,$frm_password); 
}else
{ 
	login_page(); 
}//end if 

function authenticateUser($frm_username,$frm_password)
{

		$hostname = 'localhost'; 
		$username ='myname'; 
		$password ='mypwd123'; 
		$dbName = 'my_db'; 


	$crypted_password=md5($frm_password);

	//Formulate SQL and find user
	$query="SELECT password FROM users WHERE username='$frm_username' AND password='$crypted_password'";
	echo $query;
	
	MYSQL_CONNECT($hostname, $username, $password) OR DIE("Unable to connect"); 
	@MYSQL_SELECT_DB("$dbName") OR DIE("Unable to select database");
		
	//execute the query
	$result = @mysql_query($query) or DIE(mysql_error());
	echo ("result is $result");
	$count = mysql_num_rows($result);
	echo ("count is $count");
	
	//exactly one row - then we have found the user
	if (mysql_num_rows($result)==1)
	{
		//Register the frm_username to show the user is logged in
		session_register("frm_username");
		
		//Clear any other session variables
		if (session_is_registered("errors"))
		
              //Delete the form errors session variable
		session_unregister("errors");
		
		//Redirect to a calling page?
		if(session_is_registered("referer"))
		{
			//Delete the referer session variable
			session_unregister("referer");
			
			//Then use it to redirect
			header("Location:$referer");
			exit;
		}
		else 
		{
			header("Location:orders.php");
			exit;
		}}
		else
		{
			//Ensure the username is not registered, so the user is not logged in
			if (session_is_registered("frm_username"))
				session_unregister("frm_username");
				
			//Register an error message
			session_register("message");
			$message="Username or password incorrect.".
			"login failed.";
			
			//Show the login page
			login_page();
			exit;
		}
}

function login_page()
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
</head>
<form ACTION="<?=$_SERVER['PHP_SELF'];?>" method="POST">
		     <table align="center" width="75%"  border="0" cellspacing="0" cellpadding="0">
               <tr>
			   <td>&nbsp;</td>
			   <td>&nbsp;</td>
			   </tr>
			   <tr>
			   	 <td>User Name</td>
                 <td><input type="text" name="frm_username" /></td>
               </tr>
               <tr>
                 <td>Password</td>
                 <td><input type="password" name="frm_password" /></td>
               </tr>
             </table><br />
			<input name="submit" type="submit" value="Submit" />
	         </form></body>
</html>
<?php
}
?>
Right now, when I try this code, I get the following error:
How the heck do I correct my script so that I can redirect successfully from the page where I was trying to go? I guess its a redirect forward?!

Thanks!

Posted: Wed Jun 30, 2004 9:48 pm
by tim
<a href='javascript:history.go(-1)'>click to go back 1</a>

hint maybe?

Posted: Thu Jul 01, 2004 12:32 am
by John Cartwright
$_SERVER["http_referer"]

heres a function you can use

Code: Select all

<?php
				function urlRedirect($url, $msg, $wait) 
					{ 
					$msg2 = 'Redirection In Progress.  Click here if your browser does not support automatic redirection or you do not want to wait!'; 
     				echo '<script language="javaScript"> function redirect() {window.parent.location=''' . $url . ''';}</script>'; 
     				echo '<body onload="setTimeout(''redirect()'','.$wait.');"><br>'; 
					echo $msg .'<a target="_parent" href="' . $url . '"><br><br><br>' . $msg2 . '</a></td>'; 
					} 

//note $wait would be like 3000 for 3 sec
?>
then you can call it by like

Code: Select all

<?

urlRedirect($_SERVER["http_referer"], "you have been logged in and you are being redirected to your previous page","3000");

?>

Posted: Thu Jul 01, 2004 5:15 am
by Grim...
Tim: I don't think that would work in this case, because the user never got to the page they wanted.

Posted: Thu Jul 01, 2004 5:22 am
by Grim...
Another way:

On the page the user was trying to get to, you must have something like:

Code: Select all

<?php
if (!$user_is_logged_in)
{
    header ("login.php");
    exit;
}
right?

So why not change that to

Code: Select all

<?php
if (!$user_is_logged_in)
{
    header ("login.php?page=$PHP_SELF"); //or whatever is you have register_globals off
    exit;
}
Then after a sucessful login:

Code: Select all

<?php
if ($page)
{
    header("$page");
    exit;
}
else
{
    header("index.php");
    exit;
}

Posted: Thu Jul 01, 2004 10:24 am
by feyd

Code: Select all

header('Location: http://someurl');
Grim. ;)

Posted: Thu Jul 01, 2004 9:03 pm
by dardsemail
Thanks! I ended up using a solution that may not be the best, but it works.

I set

Code: Select all

<?php$_SESSION['prevUrl']="whateverpage.php" ?>
within the page that I'm redirecting from.

Then I have been using the

Code: Select all

<?php$_SESSION['prevUrl'] ?>
as part of the header location. Like I said, not the best, but I'm new to this and it works! I'll try some more advanced methods as I get more familiar and comfortable with PHP.

Thanks for all of your great suggestions! I'll definitely come back to them!