After PHP login jump to the page before login to realize the idea and code
- 2020-12-09 00:48:09
- OfStack
Recently, A small project brought me into contact with PHP programming, simple login function and OK. However, one problem was found in the actual use: user A sent a link to user B, and when B opened the page, the user was prompted to log in. However, after the login was successful, it jumped to the home page instead of the link sent by A. For a better user experience, B should automatically redirect to the pre-login link after successful login. Check out the PHP help manual and you can do this using the $_SERVER global variable.
1 $_SERVER are PHP super global variables, a detailed explanation about $_SERVER variables can be reference: http: / / www php. net manual/zh/reserved variables. server. php
The specific implementation method is as follows: while prompting the user to log in, record the URL of the requested page in session or cookie; Jump back to URL after successful login authentication.
checklogin.php
login.php
1 $_SERVER are PHP super global variables, a detailed explanation about $_SERVER variables can be reference: http: / / www php. net manual/zh/reserved variables. server. php
The specific implementation method is as follows: while prompting the user to log in, record the URL of the requested page in session or cookie; Jump back to URL after successful login authentication.
checklogin.php
session_start();
if (!isset ($_SESSION['login_ok']))
{
echo "<script language=javascript>alert (' The page you want to visit needs to be logged in first. ');</script>";
$_SESSION['userurl'] = $_SERVER['REQUEST_URI'];
echo '<script language=javascript>window.location.href="login.php"</script>';
}
login.php
session_start();
// Omitted here account password authentication code, authentication OK Execute the following code
if (isset ($_SESSION['userurl']))
{
// There are pages in the session to jump to
$url = $_SESSION['userurl'];
}
else
{
// No page to jump to, then go to the home page
$url = "home.php";
}
//0.5s After the jump
echo "<meta http-equiv=\"refresh\" content=\"0.5;url=$url\">";