【问题标题】:PHP in Google App Engine, how to detect the url of the incoming redirect?Google App Engine中的PHP,如何检测传入重定向的url?
【发布时间】:2017-07-06 07:15:54
【问题描述】:

我不确定这是否可行,但想知道我们是否可以检测到 header('Location: ') 发起的 URL。

我有几个带有header('Location: /main.php') 的脚本,现在在执行 main.php 时,我想找出重定向是从哪个脚本或 URL 发起的。

script.php
header('Location: /main.php');

main.php
//detect the source url.. ie /script.php or website.com/script.php
// do some action
header('location: /script.php');

这可能吗?

【问题讨论】:

    标签: php google-app-engine url url-redirection


    【解决方案1】:

    我认为你可以在 main.php 上使用类似的东西。

    $referer = $_SERVER["HTTP_REFERER"];
    

    【讨论】:

    • 我认为它应该在普通服务器上工作。但我不断收到这个错误。 Notice: Undefined index: HTTP_REFERER 这似乎很奇怪。我想知道在谷歌应用引擎中运行网站是否有什么不同。
    • 该错误主要发生在 HTTP_REFERER 为空时。您可以使用 if(isset($var))if($var <> null) 之类的代码,其中 var 为 $_SERVER["HTT_REFERER"] 但我不确定这是否适用于您的场景甚至静止。
    【解决方案2】:

    我用下面的代码让它为我工作

    script.php
    $document = $_SERVER['REQUEST_URI'];
    $host = $_SERVER['HTTP_HOST'];
    $fullPath = $host.$document;
    setcookie('referer', $fullPath,time()+86400); // cookie is destroyed on redirect
    header('Location: /main.php?referer='.$fullPath);
    

    另外一个如下

    main.php
    $referer = $_REQUEST['referer'];
    if($referer != null){
            header("location: http://".urldecode($referer));
        }
    

    执行main.php 时我设置的cookie 被删除,因此我必须通过POST 发送信息。 我还必须在 $referrer 上使用 urldecode。

    这可能不是最好的方法,但它确实有效,如果有人可以改进这一点,我很乐意从中学习。

    【讨论】:

      猜你喜欢
      • 2016-07-09
      • 2015-11-20
      • 2010-11-06
      • 1970-01-01
      • 1970-01-01
      • 2012-10-11
      • 1970-01-01
      • 2012-09-22
      • 1970-01-01
      相关资源
      最近更新 更多