简体   繁体   English

登录PHP后如何重定向回

[英]How to redirect back after login in PHP

Sometime we get link from a site, when we click the invitation link, it just send us to the index page for login(as we are not logged in in that site), when the login finishes, the site just redirect us to the link on which we clicked. 有时我们从站点获得链接,当我们单击邀请链接时,它只是将我们发送到索引页面进行登录(因为我们尚未登录该站点),登录完成后,该站点会将我们重定向到该链接我们点击了

How this can be achieved in php? 如何在php中实现呢?

Let's say the "destination page" as called " a.php " ; 假设将“目标页面”称为“ a.php ”; here's a possible way of doing what you're describing : 这是您正在描述的一种可能的方式:

  • You first call a.php from your browser 您首先从浏览器调用a.php
    • a.php detects you are not logged in a.php检测到您尚未登录
    • it redirects you to index.php , with a parameter in the URL telling index.php you should be redirected to a.php when logged-in 它会将您重定向到index.php ,并在URL中使用一个参数告诉index.php您应该在登录时重定向到a.php
    • the redirection is done using the header function, with a Location header that points to something like http://yoursite.com/index.php?destination=a.php 重定向是通过header功能完成的,其中Location标头指向类似http://yoursite.com/index.php?destination=a.php
  • On index.php , you deal with the logging-in mecanism index.php ,您处理登录机制
    • ie a form, with a hidden field, in which the $_GET['destination'] is stored (properly escaped, of course) 即具有隐藏字段的表单,其中存储了$_GET['destination'] (当然可以正确转义)
    • That form posts to either index.php (ie itself) or another page ; 该表格发布到index.php (即本身)或其他页面上; doesn't quite matter. 不太重要。
  • When the logging-in form is posted : 登录表单发布后:
    • you check the credentials 您检查凭据
    • if login+password are OK : 如果login + password可以:
      • if there is a destination field in the form's data, you redirect the user to the corresponding page 如果表单的数据中有destination字段,则将用户重定向到相应的页面
      • else, you redirect the user to a default page. 否则,您将用户重定向到默认页面。
      • Of course, it might be interesting to check the content of the destination field, to make sure you are only accepting redirection to a "valid" page (the definition of "valid" can change -- but can at least mean "a page on your site") 当然,检查destination字段的内容可能很有趣,以确保您只接受重定向到“有效”页面 “有效” 的定义可以更改,但至少可以表示“您的网站”)

You might also want to try... 您可能还想尝试...

start_session();
$_SESSION[destination] = strip_tags($_GET[destination]);

..as opposed to calling the variable as a hidden field. ..而不是将变量称为隐藏字段。 Then do your check-downs from there on the POST (what check functions you need) eg 然后在POST上从那里进行检查(您需要什么检查功能),例如

if ($_SESSION[destination] !='' || empty($_SESSION[destination]) && !is_numeric($_SESSION[destination])){

header('Location:'.$_SESSION[destination]);

}

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

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