登录后重定向到上一页?PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14523468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:27:07  来源:igfitidea点击:

Redirecting to previous page after login? PHP

phpauthenticationredirect

提问by Alejandra Uzelac

I've been searching for a solution but it's seem I can't get it right, no matter what I try.

我一直在寻找解决方案,但无论我尝试什么,我似乎都无法解决问题。

After successful login, the user should be redirected to the page he came from, let's say he's been browsing a post and wants to log in so he can leave a comment, so he should be redirected to the post he was browsing. So here is what I have:

成功登录后,用户应该被重定向到他来自的页面,假设他一直在浏览一个帖子并想登录以便他可以发表评论,所以他应该被重定向到他正在浏览的帖子。所以这就是我所拥有的:

login.phpshows the login form:

login.php显示登录表单:

<form method="post" action="login-check.php">
... //input for username and password
</form>

The login-check.phpchecks if the username and pass are entered, does the user exist, or if he's already logged in, and a pparameter is sent to login.php:

登录-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();
}
?>

then parameter pis sent back to login.phpand displays the according message:

然后参数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");
}
?>

BUT, instead of going to index.php after successful login, it should go to the page the user has previously been. I've tried in different ways but ether it doesn't work at all or it returns to login.php. It doesn't need to be super safe, because I'm doing this for a school project.
ALSO, I consider myself pretty novice, so please have patience :D

但是,登录成功后,不是去 index.php,而是去用户之前访问过的页面。我尝试了不同的方法,但以太根本不起作用,或者它返回到 login.php。它不需要超级安全,因为我正在为学校项目做这件事。
另外,我认为自己很新手,所以请耐心等待:D

回答by Chris

A common way to do this is to pass the user's current page to the Login form via a $_GETvariable.

执行此操作的常用方法是通过$_GET变量将用户的当前页面传递到登录表单。

For example: if you are reading an Article, and you want to leave a comment. The URL for comments is comment.php?articleid=17. While comment.phpis loading, it notices that you are not logged in. It wants to send you to login.php, like you showed earlier. However, we're going to change your script so that is also tells the login page to remember where you are:

例如:如果您正在阅读一篇文章,并且想发表评论。评论的网址是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

This should send the user to: login.php?location=comment.php%3Farticleid%3D17. login.phpshould now check to see if $_GET['location']is populated. If it is populated, then send the user to this location (in this case, comment.php?articleid=17). For example:

这应该将用户发送到:login.php?location=comment.php%3Farticleid%3D17login.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();
}

Gotchas

陷阱

You should run some validation against $_GET['location']before sending the user there. For example, if I tell people who use your site to click on this link: login.php?location=http%3A%2F%2Fmalice.com%2Fevilpage.php... then they will be sent to a foreign URL that will try to do something bad.

$_GET['location']在将用户发送到那里之前,您应该对其进行一些验证。例如,如果我告诉使用您网站的人点击此链接:login.php?location=http%3A%2F%2Fmalice.com%2Fevilpage.php... 那么他们将被发送到一个试图做坏事的外国 URL。

Always make sure to use urlencodewhen passing URLs as $_GETparameters. This encodes special URL characters (such as ?, &, and %) so that they don't break your url (e.g.: login.php?location=comment.php?id=17<- this has two ?'s and will not work correctly)

始终确保urlencode在将 URL 作为$_GET参数传递时使用。这种编码特殊网址字符(例如?&%),以便它们不会打破你的URL(如:login.php?location=comment.php?id=17< -这样做有两个?的,将无法正常工作)

回答by Djordje Bakic

When user gets to the login page use this to see where is come from

当用户进入登录页面时,使用它来查看来自哪里

$_SERVER['HTTP_REFERER']

Then set this value into the session, and when he is authenticated use url from the session to redirect him back. But you should do some checking before, if the url is your site. Maybe he come from another site directly to login :)

然后将此值设置到会话中,当他通过身份验证时,使用会话中的 url 将他重定向回来。但是你应该先检查一下,如果 url 是你的网站。也许他直接从另一个站点来登录:)

回答by Anthony

You can save a page using php, like this:

您可以使用 php 保存页面,如下所示:

$_SESSION['current_page'] = $_SERVER['REQUEST_URI']

And return to the page with:

并返回页面:

header("Location: ". $_SESSION['current_page'])

回答by Elyor

Another way, using SESSION

另一种方式,使用 SESSION

Assign current URL to session (use it on every page)

将当前 URL 分配给会话(在每个页面上使用它)

$_SESSION['rdrurl'] = $_SERVER['REQUEST_URI'];

and in your login page, use

并在您的登录页面中,使用

if(isset($_SESSION['rdrurl']))
header('location: '.$_SESSION['rdrurl']);
else
header('location: http://example.com');

回答by Tim Martens

You should probably place the url to redirect to in a POST variable.

您可能应该将要重定向到的 url 放在 POST 变量中。

回答by philipobenito

Since the login page is a separate page, I am assuming that you want to redirect to the page that the user reached the login page from.

由于登录页面是一个单独的页面,我假设您想重定向到用户从中访问登录页面的页面。

$_SERVER['REQUEST_URI']will simply hold the current page. What you want to do is use $_SERVER['HTTP_REFERER']

$_SERVER['REQUEST_URI']将简单地保持当前页面。你想要做的是使用$_SERVER['HTTP_REFERER']

So save the HTTP_REFERER in a hidden element on your form <input type="hidden" name="referer" value="<?= $_SERVER['HTTP_REFERER'] ?>" />but keep in mind that in the PHP that processes the form you will need some logic that redirects back to the login page if login fails but also to check that the referer is actually your website, if it isn't, then redirect back to the homepage.

因此,将 HTTP_REFERER 保存在表单上的隐藏元素中,<input type="hidden" name="referer" value="<?= $_SERVER['HTTP_REFERER'] ?>" />但请记住,在处理表单的 PHP 中,您将需要一些逻辑来在登录失败时重定向回登录页面,但还要检查引用者是否实际上是您的网站,如果不是,则重定向回主页。

回答by Drew

use something like

使用类似的东西

$_SERVER['HTTP_REFERER'];

And if it's a successful login, display a link saying "Click here to go back" and a link to the referrer, and when the page loads, use some javascript to automatically load that page (don't use back() or whatever that function is as it won't re-load the page and it'll appear like the user never logged in.

如果登录成功,则显示“单击此处返回”链接和引荐来源链接,当页面加载时,使用一些 javascript 自动加载该页面(不要使用 back() 或其他任何功能是因为它不会重新加载页面,并且看起来就像用户从未登录过一样。

回答by Sandeep Garg

You can use session to to store the current page on which you want to return after login and that will work for other pages if you maintain session properly. It is very useful technique as you can develop your breadcrumb using it.

您可以使用 session 来存储登录后要返回的当前页面,如果您正确维护 session,这将适用于其他页面。这是非常有用的技术,因为您可以使用它开发面包屑。

回答by jpmaster77

You should try something like $_SERVER['HTTP_REFERER'].

你应该尝试类似的东西$_SERVER['HTTP_REFERER']

回答by Ritabrata Gautam

how about this :: javascript+php

这个怎么样 :: javascript+php

echo "<script language=javascript> javascript:history.back();</script>";

it will work same as the previous button in your browser

它将与浏览器中的上一个按钮相同