简体   繁体   English

登录后将用户重定向到上一个链接

[英]Redirect user to previous link after login

Question is quite simple. 问题很简单。 I have a PHP website and whenever a user comes in through a link that requires login, I want user to be redirected to login page and then, after he finish login, I want him to be redirected to link he tried to access when he came into the website. 我有一个PHP网站,每当用户通过需要登录的链接进入时,我希望将用户重定向到登录页面,然后,在他完成登录后,我希望将他重定向到他尝试访问时要访问的链接。进入网站。

And that's it... 就是这样...

Just pass the url you want to return to. 只需传递您要返回的网址即可。 Look at stackoverflow for example. 以stackoverflow为例。 Here is the URL I visited 这是我访问的网址

Redirect user to previous link after login 登录后将用户重定向到上一个链接

I click "log in" and I'm sent to 我单击“登录”,然后发送到

https://stackoverflow.com/users/login?returnurl=http%3a%2f%2fstackoverflow.com%2fquestions%2f25342617%2fredirect-user-to-previous-link-after-login https://stackoverflow.com/users/login?returnurl=http%3a%2f%2fstackoverflow.com%2fquestions%2f25342617%2fredirect-user-to-previous-link-after-login

If they want to send me back after I log in, then can do 如果他们想在我登录后把我发回去,那么可以

$returnurl = make_everything_safe($_GET['returnurl']);
header("Location: $returnurl")

A simple way to do this is to store the current page url in your cookies, and then when the user is logged you test if the cookie exists, if it exists then you redirect to the url in this cookie 一种简单的方法是将当前页面的URL存储在cookie中,然后在登录用户时测试cookie是否存在,如果存在,则重定向到该cookie中的url。

// Set your cookie before redirecting to the login page
$current_page = 'http://' . $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI];
$_COOKIE['redirect_to'] = $current_page;

After the user is logged 用户登录后

if(isset($_COOKIE['redirect_to']))
{
   $url = $_COOKIE['redirect_to'];
   unset($_COOKIE['redirect_to']);
   header('location:' . $url);
}

The other answers you have been given will work just fine, but here is a slightly more in depth answer that uses very simple PHP. 您得到的其他答案也可以正常工作,但这是使用更简单的PHP的更深入的答案。 No setting cookies or session variables which I would consider extra steps; 没有设置cookie或会话变量,我会考虑采取其他措施; you have to make sure to start your session on the next page for example. 例如,您必须确保在下一页上开始会话。

What I would do is have a script run that catches the page they tried to access. 我要做的是运行一个脚本,以捕获他们尝试访问的页面。 You would have to place this on every page that a visitor could land on, and then make sure to pass this value on. 您必须将其放置在访问者可以访问的每个页面上,然后确保传递此值。 Here is a great example: 这是一个很好的例子:

// Function to get the full URL even on SSL connections 
function curPageURL() {
   $pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
   $pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
   $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
   $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
   return $pageURL; // Here the full URL is returned
}

This code is from Web Cheat Sheet which has more scripts that you may want to use. 此代码来自Web备忘单 ,其中包含您可能需要使用的更多脚本。 Some are even shorter than this one. 有些甚至比这短。

Next is to keep passing this value on. 接下来是继续传递此值。 Simple urlencode the returned value (so its safe) and do this: 简单urlencode返回值(这样就很安全),并执行以下操作:

// This sends them to your login script and 
// adds the page they were heading to in the url to access with $_GET[];
header ("Location: $yourLoginPageUrl?sendBackTo=$returnedPageUrl");

Now you can do two things. 现在您可以做两件事。 The less safe is to not doing anything, let them login and on the login script simple access the value with $_GET['sendBackTo']; 不安全的做法是不执行任何操作,让他们登录,然后在登录脚本上使用$ _GET ['sendBackTo']简单访问该值; Because the URL had the value in it when they submitted the form it should still be there to access since its the reffering page. 由于URL在提交表单时具有其中的值,因此自从其引用页面以来,URL应该仍然可以访问。 NOT RECOMMENDED though. 不过不推荐。 The smart thing is to use a hidden text field on the login form and just echo the URL into so you know its there. 明智的做法是在登录表单上使用隐藏的文本字段,然后将URL回显到该位置即可。 Then after a successful login send them to it. 成功登录后,将其发送给它。

You still will need to handle catching log ins with no recorded URL to go back to. 您仍然需要处理没有记录的URL的捕获登录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM