View Full Version : Signin page to direct user back to their original page

12-22-2010, 12:12 AM
I am currently creating a website and am trying to make it so that once a user logs in they are taken back to the page they were on prior to signing in. I am unfortunately having difficulty, I can get the page that the user was on before fine but when I try to send the user back to that page after successful log in it doesn't work...please help.

Any help is much appreciated.

My php code for the signin page is as follows:



//posted back from the signin form
$username = $_POST['username'];
$password = $_POST['password'];
$submit = $_POST['signinsubmit'];

//Gets the page that the user was on previously
$page = $_GET['page'];

if($submit != null)
if($password == null)
$passwordcheck = "failed";

if($username == null)
$usernamecheck = "failed";

if($passwordcheck == null && $usernamecheck == null)
$sqlquery= "SELECT * FROM User WHERE Username = '$username' AND Password = md5('$password')";

$sqlresult = mysql_query($sqlquery);
$sqlcheck = mysql_fetch_array($sqlresult);

//If username and password are correct then do the following
if($sqlcheck['Username'] <> NULL)
//assign session
$_SESSION['user'] = $username;

//send user to previous page...not working
header("location: " . $page);
$signinfailed = yes;

12-22-2010, 02:02 AM
Your problem lies with this line:
$page = $_GET['page'];

You are getting to the sign-in page via a <form> with method="post",
so you need to use $_POST['page'] ... but that's not the only thing
to change ...

On the page where they came from, you need to find out the name of the page
(or script) they are currently on. This will give you the name, such as "index.php",
or "contact.php" ... only the script name, not the directory or path.

$script = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);

Then ...

Your form would look something like this ... with the hidden page value ...

<?php $script = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1); ?>
<form action="signin.php" method="post">
<input type="hidden" name="page" value="<?=$script?>">
Username: <input type="text" name="username"><br />
Password: <input type="text" name="password"><br />
<input type="submit" name="signinsubmit" value="Sign In">