:o
First of all, my apologies for having two threads... but this was a totally different issue than my other topic.
If a user requests a protected page and is prompted to login (using sessions), how is it possible to allow that login to send him/her right back to the orginal page they wanted after the login?
While searching the forum... I've found the logic is this:
If the login function (on the protected page) can't authenticate the user, it can present a login form
instead of the protected URL, posting back to your login.php with a hidden form
field containing the URL. On successful login, it can issue a "Location" header
back to the URL saved from earlier.
From my readings... I think this can be accomplished somehow using: $_SERVER['HTTP_REFERER']
But I am not clear about where to use that in my code.
The header on the "protected" page that checks to see if user is logged in:
<?php
session_start();
if (!isset($_SESSION['db_is_logged_in']) || $_SESSION['db_is_logged_in'] !== true) {
header('Location: login.php');
exit;
}
?>
and here's the code on the login page... which at the moment CLEARLY defines the browser-out page using a header as "main.php"... I'd like to change this to redirect to whatever the page that was just previously requested (rather than write a million specific login pages):
<?php
// we must never forget to start the session
session_start();
MySQL_connect("localhost", 'username', 'password'); MySQL_select_db("members");
$errorMessage = '';
if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) {
$userId = $_POST['txtUserId'];
$password = $_POST['txtPassword'];
// check if the user id and password combination exist in database
$sql = "SELECT user_id
FROM members
WHERE user_id = '$userId' AND user_password = PASSWORD('$password')";
$result = mysql_query($sql) or die('Query failed. ' . mysql_error());
if (mysql_num_rows($result) == 1) {
// the user id and password match,
// set the session
$_SESSION['db_is_logged_in'] = true;
// after login we move to the main page
header('Location: main.php');
exit;
} else {
$errorMessage = 'Sorry, wrong user id / password';
}
}
?>
Thanks a million...
~Wayne