引荐来源网址在重定向后消失

问题描述 投票:0回答:1

我有一个重定向脚本,该脚本进行一些跟踪,然后将用户重定向到目标。看起来像这样

class Redirect() {
  private function init() {
    // analyze parameters
    (...)

    $this->referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';

    $this->trackVisit($this->referer, $someMoreData);

    $destination = $this->getUrlByParameters(...);

    $this->redirectUrl = $destination;
  }

  private function run() {
    (...)
    header('Location: ' . $this->redirectUrl);
    (...)
  }
}

$r = new Redirect();
if($r->init()) {
  $r->run();
}

让我头疼的是,我可以在服务器上看到引荐来源网址,并将其保存到我的数据库中,但是在重定向用户后它消失了。目标和所有可能的后续重定向都不再具有引荐来源网址。

Referrer-Policy设置为'no-referrer-when-downgrade',但我总是重定向到https,所以这不应该成为问题。

我希望有人可以帮助我!

php redirect http-headers referrer
1个回答
0
投票

如果使用header("Location:...")从脚本重定向请求,将不保留eferrer。您可以将引用作为变量传递给查询字符串:

header('Location: ' . $this->redirectUrl."?ref=".$_SERVER['HTTP_REFERER']);

或&,如果URL已经包含一些参数。然后,您可以使用$_GET["ref"]

在其他脚本上获得原始引荐来源

或者您可以自己编写标题,并将其附加在Location标题之前,然后发送完整的标题。

© www.soinside.com 2019 - 2024. All rights reserved.