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

时间:2022-10-20 10:09:12

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.php shows the login form:

登录。php显示登录表单:

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

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

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 is sent back to login.php and displays the according message:

p被发送回登录。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

但是,不是索引。php登录成功后,应该转到用户之前的页面。我尝试了不同的方法,但是以太根本不起作用,否则返回login.php。它不需要是超级安全的,因为我做这个是为了一个学校项目。另外,我认为自己是新手,所以请有耐心:D。

17 个解决方案

#1


57  

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

一种常见的方法是通过$_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.php is 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:

例如:如果你正在阅读一篇文章,你想留下评论。注释的URL是comments .php?articleid=17。而评论。php正在加载,它注意到您没有登录。它想让你登录。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.php should 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%3D17。登录。php现在应该检查是否填充了$_GET['location']。如果它被填充,则将用户发送到这个位置(在本例中是注释.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%2F %2Fmalice.com%2Fevilpage.php…然后它们将被发送到一个试图做坏事的外国URL。

Always make sure to use urlencode when passing URLs as $_GET parameters. 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)

在将url作为$_GET参数传递时,一定要使用urlencode。它编码特殊的URL字符(如?、&和%),这样它们就不会破坏您的URL(例如:login.php?location=comment.php?id=17 <-这有两个?不能正常工作)

#2


8  

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是您的站点,您应该进行一些检查。也许他是从别的网站直接登录的:)

#3


6  

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'])

#4


3  

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

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

#5


3  

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保存在窗体的一个隐藏元素中,但请记住,在PHP处理你需要一些逻辑形式,重定向回登录页面,如果登录失败,还要检查推荐人是你的网站,如果不是,然后重定向回主页。

#6


2  

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自动加载该页面(不使用回()之类的函数,因为它不会重新加载代码页面,就会出现像用户没有登录。

#7


2  

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.

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

#8


2  

Another way, using 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');

#9


1  

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

您应该尝试$_SERVER['HTTP_REFERER']之类的东西。

#10


0  

Construct the form action such that it 'remembers', or persists, the previous page by writing out a returnurl=value query string key/value pair to the URL - this can be passed from any page that redirects to login.

构造表单动作,使其“记住”或持久保存前一页,将returnurl=value查询字符串键/值对写入URL——这可以从任何重定向到登录的页面传递。

#11


0  

I think you might need the $_SERVER['REQUEST_URI'];

我认为您可能需要$_SERVER['REQUEST_URI'];

if(isset($_SESSION['id_login'])) {
  header("Location:" . $_SERVER['REQUEST_URI']);
}

That should take the url they're at and redirect them them after a successful login.

这将获取它们所在的url,并在成功登录后重定向它们。

#12


0  

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

它将与浏览器中的上一个按钮一样工作

#13


0  

if(isset($_SESSION['id_login'])) {
  header("Location:" . $_SERVER['REQUEST_URI']);
}

I Used this code and it works. Thanks alot.

我使用了这段代码,它可以工作。谢谢你很多。

#14


0  

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

And return to the page with:

返回页面:

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

This code works.

这段代码。

#15


0  

Use hidden input in your login page. Like:

在登录页面中使用隐藏的输入。如:

<input name="location" value="<?php if(!empty($_SERVER['HTTP_REFERER'])) echo $_SERVER['HTTP_REFERER']; else echo 'products.php'; ?>" type="text" style="display: none;" />

#16


0  

You should first get user refer page in a variable using $_SERVER['HTTP_REFERER']; in your login page.

您应该首先使用$_SERVER['HTTP_REFERER']在变量中获取用户引用页面;在你的登录页面。

LIKE:

如:

<?php 
    session_start();
    $refPage = $_SERVER['HTTP_REFERER']; 
?>

And now when the user clicks to Login then change header location to user refer page

现在,当用户单击登录时,将标题位置更改为用户引用页面

LIKE:

如:

<?php 
if(isset($_POST[login])){
    session_start();
    header('location:' . $refPage);
}
?>

And in this time you should first check that user refers page empty or not because your user can visit direct your login page then your $refPage variable will be empty so after Click to Login page stays here

在这段时间里,你应该首先检查那个用户是否为空,因为你的用户可以访问你的登录页面,然后你的$refPage变量将是空的,所以点击后,登录页面就会停留在这里。

LIKE:

如:

<?php
if(isset($_POST[login])){
    session_start();
    $refPage = $_SERVER['HTTP_REFERER'];  // get reffer page url
    if(empty($refPage)){ 
        header('location: yourredirectpage'); // if ref page is empty then set default redirect page.
    }else{
        header('location:' . $refPage); // or if ref page in not empty then redirect page to reffer page
    }
}
?>


Or you can use input type hidden where you can set value $_SERVER['HTTP_REFERER'];

或者您可以使用隐藏的输入类型来设置值$_SERVER['HTTP_REFERER'];

LIKE:

如:

<input type="hidden" name="refPage" value="<?php echo $_SERVER['HTTP_REFERER']; ?>">

And when a user clicks to Login then you can get the refPage value and redirect the previous page. And you should also check empty refer page. Because your user can visit direct your login page.

当用户单击登录时,您可以获取refPage值并重定向上一个页面。你也应该检查空的参考页面。因为用户可以直接访问登录页面。


Thank you.

谢谢你!

#17


-1  

@philipobenito's answer worked best for me.
I first created a hidden input that contain the user's HTTP referer

@philipobenito的答案最适合我。我首先创建了一个隐藏的输入,其中包含用户的HTTP referer。

<input type="hidden" name="referer" value="<?= $_SERVER['HTTP_REFERER'] ?>" />

and after a successful login i redirected the users to whatever value was stored in that hidden input

成功登录后,我将用户重定向到隐藏输入中存储的任何值

$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
if(!empty($_POST['referer'])){
    header('Location: '.$_POST['referer']);
}
else{
    header('Location: members.php'); //members.php is a page used to send a user to their profile page.
}
exit;

#1


57  

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

一种常见的方法是通过$_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.php is 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:

例如:如果你正在阅读一篇文章,你想留下评论。注释的URL是comments .php?articleid=17。而评论。php正在加载,它注意到您没有登录。它想让你登录。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.php should 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%3D17。登录。php现在应该检查是否填充了$_GET['location']。如果它被填充,则将用户发送到这个位置(在本例中是注释.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%2F %2Fmalice.com%2Fevilpage.php…然后它们将被发送到一个试图做坏事的外国URL。

Always make sure to use urlencode when passing URLs as $_GET parameters. 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)

在将url作为$_GET参数传递时,一定要使用urlencode。它编码特殊的URL字符(如?、&和%),这样它们就不会破坏您的URL(例如:login.php?location=comment.php?id=17 <-这有两个?不能正常工作)

#2


8  

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是您的站点,您应该进行一些检查。也许他是从别的网站直接登录的:)

#3


6  

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'])

#4


3  

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

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

#5


3  

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保存在窗体的一个隐藏元素中,但请记住,在PHP处理你需要一些逻辑形式,重定向回登录页面,如果登录失败,还要检查推荐人是你的网站,如果不是,然后重定向回主页。

#6


2  

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自动加载该页面(不使用回()之类的函数,因为它不会重新加载代码页面,就会出现像用户没有登录。

#7


2  

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.

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

#8


2  

Another way, using 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');

#9


1  

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

您应该尝试$_SERVER['HTTP_REFERER']之类的东西。

#10


0  

Construct the form action such that it 'remembers', or persists, the previous page by writing out a returnurl=value query string key/value pair to the URL - this can be passed from any page that redirects to login.

构造表单动作,使其“记住”或持久保存前一页,将returnurl=value查询字符串键/值对写入URL——这可以从任何重定向到登录的页面传递。

#11


0  

I think you might need the $_SERVER['REQUEST_URI'];

我认为您可能需要$_SERVER['REQUEST_URI'];

if(isset($_SESSION['id_login'])) {
  header("Location:" . $_SERVER['REQUEST_URI']);
}

That should take the url they're at and redirect them them after a successful login.

这将获取它们所在的url,并在成功登录后重定向它们。

#12


0  

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

它将与浏览器中的上一个按钮一样工作

#13


0  

if(isset($_SESSION['id_login'])) {
  header("Location:" . $_SERVER['REQUEST_URI']);
}

I Used this code and it works. Thanks alot.

我使用了这段代码,它可以工作。谢谢你很多。

#14


0  

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

And return to the page with:

返回页面:

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

This code works.

这段代码。

#15


0  

Use hidden input in your login page. Like:

在登录页面中使用隐藏的输入。如:

<input name="location" value="<?php if(!empty($_SERVER['HTTP_REFERER'])) echo $_SERVER['HTTP_REFERER']; else echo 'products.php'; ?>" type="text" style="display: none;" />

#16


0  

You should first get user refer page in a variable using $_SERVER['HTTP_REFERER']; in your login page.

您应该首先使用$_SERVER['HTTP_REFERER']在变量中获取用户引用页面;在你的登录页面。

LIKE:

如:

<?php 
    session_start();
    $refPage = $_SERVER['HTTP_REFERER']; 
?>

And now when the user clicks to Login then change header location to user refer page

现在,当用户单击登录时,将标题位置更改为用户引用页面

LIKE:

如:

<?php 
if(isset($_POST[login])){
    session_start();
    header('location:' . $refPage);
}
?>

And in this time you should first check that user refers page empty or not because your user can visit direct your login page then your $refPage variable will be empty so after Click to Login page stays here

在这段时间里,你应该首先检查那个用户是否为空,因为你的用户可以访问你的登录页面,然后你的$refPage变量将是空的,所以点击后,登录页面就会停留在这里。

LIKE:

如:

<?php
if(isset($_POST[login])){
    session_start();
    $refPage = $_SERVER['HTTP_REFERER'];  // get reffer page url
    if(empty($refPage)){ 
        header('location: yourredirectpage'); // if ref page is empty then set default redirect page.
    }else{
        header('location:' . $refPage); // or if ref page in not empty then redirect page to reffer page
    }
}
?>


Or you can use input type hidden where you can set value $_SERVER['HTTP_REFERER'];

或者您可以使用隐藏的输入类型来设置值$_SERVER['HTTP_REFERER'];

LIKE:

如:

<input type="hidden" name="refPage" value="<?php echo $_SERVER['HTTP_REFERER']; ?>">

And when a user clicks to Login then you can get the refPage value and redirect the previous page. And you should also check empty refer page. Because your user can visit direct your login page.

当用户单击登录时,您可以获取refPage值并重定向上一个页面。你也应该检查空的参考页面。因为用户可以直接访问登录页面。


Thank you.

谢谢你!

#17


-1  

@philipobenito's answer worked best for me.
I first created a hidden input that contain the user's HTTP referer

@philipobenito的答案最适合我。我首先创建了一个隐藏的输入,其中包含用户的HTTP referer。

<input type="hidden" name="referer" value="<?= $_SERVER['HTTP_REFERER'] ?>" />

and after a successful login i redirected the users to whatever value was stored in that hidden input

成功登录后,我将用户重定向到隐藏输入中存储的任何值

$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
if(!empty($_POST['referer'])){
    header('Location: '.$_POST['referer']);
}
else{
    header('Location: members.php'); //members.php is a page used to send a user to their profile page.
}
exit;