Refering a page that called a particular page
-
26-05-2021 - |
Question
I have an index page with three links as follws. When I click the first link (pageType1 in the example below), pageType1.php is run that first check if the user is logged in via cookie. If it is not then redirect the user to a login page. when a user submit login and password info on login page correctly, I want to display the info on pageType1.php
FILE: index.php
<a href="pageType1.php" target="_blank">PageType1</a>
<a href="pageType2.php" target="_blank">PageType2</a>
<a href="pageType3.php" target="_blank">PageType3</a>
FILE : pageType1.php
<?php
include"auth/auth_check_header.php";
print("pageType1 contents");
?>
FILE : pageType2.php
<?php
include"auth/auth_check_header.php";
print("pageType2 contents");
?>
FILE : pageType3.php
<?php
include"auth/auth_check_header.php";
print("pageType3 contents");
?>
FILE : auth/auth_check_header.php
$successful_login_url = ?????? //How to track this value.
if (!cookie_enabled)
{
//login page
header("location:$successful_login_url");
}
My question is how to track "$successful_login_url" value (Or what shoud I put as the value of this variable) in auth/auth_check_header.php
Note: I tried putting $_SESSION["referer"] as the value of "$successful_login_url" but it shows index.php after successful login. Thanks in advance
La solution
1) User try to access a pageType1.php
2) The user is redirected to the login.php which contains a form with the hidden field:
<input type="hidden" name="redirectTo" value="<?php echo $_SESSION["HTTP_REFERER"]?>" />
3) User type the login and when successfully authenticated:
$redirectTo = $_POST['redirectTo']
4) If $redirectTo
is not empty, then redirect, else redirect to a default page
Autres conseils
You can pass in the page url as a GET variable (query string) and redirect based on this or if you want to get the page the include is in, you can use 'PHP_SELF' and store this in a variable and use it to redirect back:
$_SERVER['PHP_SELF']