简体   繁体   English

登录后如何将用户重定向回所需的URL(php)

[英]how to redirect the user back to desired URL after login (php)

user clicked the link 用户点击了链接

http:://www.myapp.com/anypage.php he is not logged then he should be redirect to http://www.myapp.com/anypage.php他没有登录,则应将其重定向到

http://www.myapp.com/login.php http://www.myapp.com/login.php

how to redirect him back to http:://www.myapp.com/anypage.php after success login 成功登录后如何将他重定向回http://www.myapp.com/anypage.php

i do this 我这样做

if (strpos($_SERVER["HTTP_REFERER"],'myapp.com') !== false && strpos($_SERVER["HTTP_REFERER"],'login')  !== false) {
                            //echo found

                                //$this->redirect($_SERVER["HTTP_REFERER"]);
                            }

i know this error but i could not found other solution as i will read HTTP_REFERER to login page 我知道此错误,但是我找不到其他解决方案,因为我将读取HTTP_REFERER进入登录页面

After login, you can change the http header to a different location to create a redirect. 登录后,您可以将http标头更改为其他位置以创建重定向。

if(Successful Login) {
    header("location: success.php");
    exit(); //to stop script 
}
else { //unsuccessful
    header("location: login.php");
    exit(); 
}

Just make sure this code goes before any html markup, or before you echo/print anything out with php. 只需确保此代码在任何html标记之前,或者在您使用php回显/打印任何内容之前进行此操作即可。

To get the script to redirect to the page the user was on before their session expired combine the above with the following: 为了使脚本重定向到用户在其会话过期之前所在的页面,请将上述内容与以下内容结合起来:

This next line of code must be AFTER where you check for a timeout in your script. 下一行代码必须在脚本检查超时之后。

Get the current page and store it in the session: 获取当前页面并将其存储在会话中:

$_SESSION['page'] = __FILE__;

In the part of the script where we check if the login has expired, which must be before the line of code above, we can pull out what was the last page from the 'page' field in the session, and pass it in the query string back to the login page. 在脚本中我们检查登录是否已到期的部分(必须在上面的代码行之前),我们可以从会话的“页面”字段中提取最后一页,并将其传递给查询字符串返回登录页面。 This means that if the user logs in successfully again, you can pull the page they were on from the query string and redirect them from it. 这意味着,如果用户再次成功登录,则可以从查询字符串中拉出他们所在的页面,并从中重定向页面。

To check for a timeout 检查超时

if(user login is timed out) {

    $header = "login.php?redirect=".$_SESSION['page']; //won't have been changed to the current page yet.
    header("location: ".$header);

}

On login.php or whatever your login page is, check if the redirect field is set in the query string. 在login.php或您登录页面上的任何页面上,检查是否在查询字符串中设置了重定向字段。 If it is you could add it as a hidden field in your form 如果是,则可以将其添加为表单中的隐藏字段

if(isset($_GET['redirect'])) {

    echo "<input name='redirect' type='hidden' value='".$_GET['redirect']."' />";

}

And finally on the script where you validate your login, you could check to see if the redirect field was present or set. 最后,在验证登录名的脚本上,您可以检查是否存在或设置了重定向字段。

If you posted the form you could do: 如果您发布了表格,则可以执行以下操作:

//Validate login

if(valid login) {

   //check if redirect was set
   if(isset($_POST['redirect'])) {

       header("location: ".$_POST['redirect']);
       exit();

   }
   else
     //go to normal landing page

}

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

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