How do I Forward URL to page so that it can send user back?
Posted: Thu May 15, 2008 5:18 pm
I am trying to forward the URL of a page requested by a user to a page such as a login page. From there the user could login and then be sent back to the page that they originally requested.
For example, if the the user was to click on a link to "member data" they wouldn't be able to view that page until they had logged in. Therefore, the user validations would send them to the login screen. From there though, I would like the user to be sent back to the page they wanted originally ("member data").
Currently, it forwards the user to the homepage; however, I would love for it to send them back to the page they originally requested.
To my knowledge the best way to do this would be to forward the URL of the original page to the login page. Then the login page could simply redirect the user to that page now that they have logged in.
This is my current code:
User Validation code at top of "Member Pages":
Login Page:
//Just a simple page with a password and username field and a submit button
//It send the data to the login script page below
Login Script:
How would I accomplish what I am trying to do using the code I currently have? Thanks in advance for any help!
For example, if the the user was to click on a link to "member data" they wouldn't be able to view that page until they had logged in. Therefore, the user validations would send them to the login screen. From there though, I would like the user to be sent back to the page they wanted originally ("member data").
Currently, it forwards the user to the homepage; however, I would love for it to send them back to the page they originally requested.
To my knowledge the best way to do this would be to forward the URL of the original page to the login page. Then the login page could simply redirect the user to that page now that they have logged in.
This is my current code:
User Validation code at top of "Member Pages":
Code: Select all
<?php
session_start();
if ($_SESSION['memberLoggedIn'] != 1)
{
$URL="http://localhost/hr-bin/login.php";
header ("Location: $URL");
}
?>Login Page:
//Just a simple page with a password and username field and a submit button
//It send the data to the login script page below
Login Script:
Code: Select all
<?php
session_start();
// store session data
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test_db", $con);
$result = mysql_query("SELECT * FROM members WHERE userName='$_POST[userName]' LIMIT 1") or die(mysql_error());
$row = mysql_fetch_assoc( $result );
$user_ID = $row['user_ID'];
$passvalidate = $row['password'];
if ($_POST[password] == $passvalidate)
{
$_SESSION['loggedin'] = 1;
$_SESSION['user_ID'] = $user_ID;
//PAGE REDIRECT TO INDEX
$URL="http://localhost/project13/index.php";
header ("Location: $URL");
}
else
{
echo "Incorrect password or username combination...please press back and login again<br>Please check to see if CAPS Lock is on.<br><br>
NOTE: If you continue to reach this page, please go back to the login page and click 'Forgot my Password'.";
}
mysql_close($con);
?>