重定向到用户登录后试图访问的页面 [英] Redirect to page user was trying to get to after logging in
问题描述
一直在阅读以找到答案,但运气并不好.
Been reading a bit to find the answer with not too much luck.
我有一个网站,会员可以匿名浏览该网站,但某些页面受到限制.一旦他们单击需要他们登录才能查看的链接,我就会将成员重定向到登录页面.
I have a site where members can browse the site anonymously but some pages are restricted. I have the members redirected to a login page once they click a link that needs them to be logged in to view.
我面临的问题是我不知道如何将会员重定向到他们登录后试图访问的页面.
The problem I'm facing is I don't know how to redirect the member to the page they were trying to get to once logged in.
他们试图访问/profile.phtml ,它把他们发送到/login.phtml ,现在当他们登录时,我希望他们被发送到/profile.phtml 因为那是他们点击到达的地方.如果他们点击/album.phtml,我希望他们在登录后被发送到/album.phtml.
They tried to to get to /profile.phtml , it sent them to /login.phtml , now when they log in, I want them to be sent to /profile.phtml because that's where they clicked to get to. If they clicked /album.phtml, I want them to be sent to /album.phtml after login.
有人可以帮忙吗?我是否以某种方式将 URL 存储在会话中?
Can anyone help? Do I somehow store the URL in a session?
非常感谢.
推荐答案
在调用 header("Location:")
进行重定向之前,将他们当前所在的页面存储在 $_SESSION['redirect_to']
.成功登录后,再次调用 header()
以重定向回原始页面并取消设置会话变量,以免在任何地方意外重用.
Just before making the header("Location:")
call to redirect, store the page they're currently on in $_SESSION['redirect_to']
. Upon successful login, make another header()
call to redirect back to the original page and unset the session variable so it doesn't get accidentally reused anywhere.
$_SESSION['redirect_to'] = $_SERVER['REQUEST_URI'];
header("Location: http://example.com/login.php");
exit();
// On successful login
$redirect = $_SESSION['redirect_to'];
// unset the session var
unset($_SESSION['redirect_to']);
header("Location: http://example.com/$redirect");
exit();
这篇关于重定向到用户登录后试图访问的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!