dongshen4129 2014-08-12 14:01
浏览 130
已采纳

如何在登录后将用户重定向回所需的URL(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/login.php

how to redirect him back to http:://www.myapp.com/anypage.php after success login

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

  • 写回答

1条回答 默认 最新

  • dougu5886 2014-08-12 14:06
    关注

    After login, you can change the http header to a different location to create a redirect.

    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.

    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. 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
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 qgcomp混合物线性模型分析的代码出现错误:Model aliasing occurred
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'