Here's the goal. After the user logs in on my login.php page I either redirect them by default to the "account.php" page OR redirect them to the last protected page they were trying to access.
Here's what I'm doing.
Using $_SESSION["username"] I'm verifying whether or not the user is logged in with my confirm_user_logged_in function below. If they are logged in, they go to the requested page. If not, I'm setting the page they came from as the $_GET variable 'last_page'.
function confirm_user_logged_in() {
if(!logged_in ()) {
$last_page = $_SERVER['PHP_SELF'];
$last_page_trim = ltrim ($last_page, '/');
$redirect_url = "login.php?last_page=" . $last_page_trim;
redirect_to($redirect_url);
}
}
function logged_in () {
return isset($_SESSION["username"]);
}
function redirect_to($new_location) {
header("Location: " . $new_location);
exit;
}
This part works. If I go to my login.php page from "example.php" the $_GET['last_page'] value is set and I get the following in the url string.
http://example.com/login.php?last_page=example.php
Next... On the login.php page I'm testing to see whether or not $_GET['last_page'] isset.
if (isset($_GET['last_page'])) {
$last_page = $_GET['last_page'];
} else {
$last_page = "account.php";
}
At this point if I echo $last_page I get the following result.
example.php
Here's the problem. If I call my "redirect_to" function it's always sending the user to account.php. Even though $_GET['last_page'] isset AND even though $last_page is set to "example.php", I'm still being sent to account.php. Here's the redirect_to function as called.
redirect_to($last_page);
Why is $last_page dropping the $_GET['last_page'] value as soon as I put it into the "redirect_to" function?
Thanks!