简体   繁体   English

使用PHP登录后,如何将用户重定向到他们的上一页?

[英]How do you redirect the user to their previous page after logging in with PHP?

I have a log in widget included on every page on my website. 我的网站的每个页面上都包含一个登录小部件。 Currently when they log in, they are redirected to the home page. 当前,当他们登录时,他们被重定向到主页。 However I want it so when they log in, they stay on the page they are currently viewing. 但是我想要这样,当他们登录时,他们停留在当前正在查看的页面上。

On my forum you have to be logged in to post (obviously). 在我的论坛上,您必须登录才能发布(显然)。 I would like it so they will stay on the forum post they are trying to reply to after logging in, rather than having to find it again. 我希望这样做,这样他们就可以留在论坛上,他们尝试在登录后回复,而不必再次查找。 How do I do this? 我该怎么做呢?

The simplest (albeit not completely reliable way is to use HTTP_REFERER and redirect to the referer page. You might need to pass this around a bit in case your login action spans multiple page. 最简单的方法(尽管并非完全可靠)是使用HTTP_REFERER并重定向到引用页面。如果您的登录操作跨越多个页面,则可能需要进行一些传递。

The more proper way is to set the current (unlogged) page in session and redirect to that session value page on login. 更正确的方法是在会话中设置当前(未记录)页面,并在登录时重定向到该会话值页面。

It depends on the case but a couple options come to mind; 这要视情况而定,但是会想到一些选择。

  • Having a redirect parameter that will redirect the user once he logs in. 具有重定向参数,该参数将在用户登录后将其重定向。
  • Using the HTTP_REFERER to refer the user back to where he came from. 使用HTTP_REFERER将用户HTTP_REFERERHTTP_REFERER
  • Depending on the login form; 取决于登录表单; you could send an ajax request to login the user without moving him 您可以发送ajax请求以登录用户而不移动他

But I think it ultimately depends on your environment and since you haven't provided any information/code other than my forum I can only be as vague as your question. 但是我认为这最终取决于您的环境,并且由于您除了我的论坛以外没有提供其他任何信息/代码,所以我只能像您的问题那样模糊。

You can bind your current page inside your login widget inside a hidden field and tell the authentication page to redirect to this binded value as a page after login success . 您可以将当前页面绑定在隐藏字段内的登录窗口小部件中,并告诉认证页面成功登录后作为页面重定向到此绑定值。

Or if you want to be more secure try using sessions and bing the current page inside it then extract the variable binded into this session in your authentication page then redirect to it as a valid page 或者,如果您想更安全,请尝试使用sessions并在其中绑定当前页面,然后在身份验证页面中提取绑定到该会话的变量,然后将其重定向为有效页面

and you can also check this variable if is a valid page by using file_exists so plz try that and tell me the result 您还可以使用file_exists来检查此变量是否为有效页面,因此请尝试并告诉我结果

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

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