Page 1 of 1

How do I Forward URL to page so that it can send user back?

Posted: Thu May 15, 2008 5:18 pm
by mirra1515
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":

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);
?>
How would I accomplish what I am trying to do using the code I currently have? Thanks in advance for any help!

Re: How do I Forward URL to page so that it can send user back?

Posted: Thu May 15, 2008 5:26 pm
by EverLearning
On your "member data" page, when user tries to access it and isn't logged in, set a session variable e.g. $_SESSION['redirect_url'] to the url of that page("member data"), and redirect the user to the login screen, like you're doing now.
In login script, after a successful login, check to see if the $_SESSION['redirect_url'] is set, and if it is, do a redirect to the url in that variable.