成功登录后,用户应该被重定向到他来自的页面,假设他一直在浏览帖子并想登录以便他可以发表评论,那么他应该被重定向到他正在浏览的帖子。所以这就是我所拥有的:
login.php 显示登录表单:
<form method="post" action="login-check.php">
... //input for username and password
</form>
login-check.php 检查是否输入了用户名和密码,用户是否存在,或者他是否已经登录,并发送一个 p
参数到 login.php:
<?php
session_start();
if((empty($username) OR empty($password) AND !isset($_SESSION['id_login']))) {
header("Location:login.php?p=1");
exit();
}
elseif (!user_exists($username,$password) AND !isset($_SESSION['id_login'])) {
header("Location:login.php?p=2");
exit();
}
elseif(isset($_SESSION['id_login'])) {
header("Location:login.php?p=3");
exit();
}
?>
然后参数 p
被发送回 login.php 并显示相应的消息:
<?php
if(isset($_GET['p'])) {
$p = $_GET["p"];
if($p=="1")
echo "<p class=\"red\">You didn't fill the form.</p><br></br>";
if($p=="2")
echo "<p class=\"red\">User exists.</p><br></br>";
if($p=="3")
header("Location: index.php");
}
?>
但是,成功登录后不应转到 index.php,它应该转到用户之前访问过的页面。我尝试过不同的方法,但它要么根本不起作用,要么返回到 login.php。
最佳答案
执行此操作的常用方法是通过 $_GET
变量将用户的当前页面传递给登录表单。
例如:如果您正在阅读一篇文章,并且想要发表评论。评论的 URL 是 comment.php?articleid=17
。当 comment.php
正在加载时,它会注意到您没有登录。它想将您发送到 login.php
,就像您之前展示的那样。但是,我们将更改您的脚本,以便让登录页面记住您的位置:
header("Location:login.php?location=" . urlencode($_SERVER['REQUEST_URI']));
// Note: $_SERVER['REQUEST_URI'] is your current page
这应该将用户发送到:login.php?location=comment.php%3Farticleid%3D17
。 login.php
现在应该检查是否填充了 $_GET['location']
。如果已填充,则将用户发送到此位置(在本例中为 comment.php?articleid=17
)。例如:
// login.php
echo '<input type="hidden" name="location" value="';
if(isset($_GET['location'])) {
echo htmlspecialchars($_GET['location']);
}
echo '" />';
// Will show something like this:
// <input type="hidden" name="location" value="comment.php?articleid=17" />
// login-check.php
session_start();
// our url is now stored as $_POST['location'] (posted from login.php). If it's blank, let's ignore it. Otherwise, let's do something with it.
$redirect = NULL;
if($_POST['location'] != '') {
$redirect = $_POST['location'];
}
if((empty($username) OR empty($password) AND !isset($_SESSION['id_login']))) {
$url = 'login.php?p=1';
// if we have a redirect URL, pass it back to login.php so we don't forget it
if(isset($redirect)) {
$url .= '&location=' . urlencode($redirect);
}
header("Location: " . $url);
exit();
}
elseif (!user_exists($username,$password) AND !isset($_SESSION['id_login'])) {
$url = 'login.php?p=2';
if(isset($redirect)) {
$url .= '&location=' . urlencode($redirect);
}
header("Location:" . $url);
exit();
}
elseif(isset($_SESSION['id_login'])) {
// if login is successful and there is a redirect address, send the user directly there
if($redirect) {
header("Location:". $redirect);
} else {
header("Location:login.php?p=3");
}
exit();
}
陷阱
在将用户发送到那里之前,您应该对 $_GET['location']
运行一些验证。例如,如果我告诉使用您网站的人点击此链接:login.php?location=http%3A%2F%2Fmalice.com%2Fevilpage.php
... 那么他们将发送到会尝试做坏事的外部 URL。
在将 URL 作为 $_GET
参数传递时,请务必使用 urlencode
。这会对特殊的 URL 字符(例如 ?
、&
和 %
)进行编码,以便它们不会破坏您的 url(例如:login.php?location=comment.php?id=17
<- 这有两个 ?
并且不能正常工作)
关于php - 登录后重定向到上一页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14523468/