php 中的引用 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2069804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:54:14  来源:igfitidea点击:

Referral URL in php

php

提问by user220755

So I am trying to get the page where a visitor came from. I inserted this code into a php file and I am trying to see the page's URL but it is not working, any suggestions?

所以我试图获取访问者来自的页面。我将此代码插入到一个 php 文件中,我试图查看页面的 URL,但它不起作用,有什么建议吗?

<?php 

  $ref = getenv("HTTP_REFERER"); 
  echo $ref; 

?>

(added this after some answers) I have also tried

(在一些答案之后添加了这个)我也尝试过

print $_SERVER["HTTP_REFERER"];

and that doesn't work either

这也不起作用

it worked after i updated the website many times, not sure why was there a problem in the first place, thanks anyway :)

在我多次更新网站后它起作用了,不知道为什么首先会出现问题,无论如何谢谢:)

回答by Sampson

Have you tried accessing through the $_SERVERsuperglobal?

您是否尝试过通过$_SERVER超全局访问?

print $_SERVER["HTTP_REFERER"];

回答by Evert

$_SERVER['HTTP_REFERER'] is the best way to access this information.

$_SERVER['HTTP_REFERER'] 是访问此信息的最佳方式。

Based on your comments on other responses:

根据您对其他回复的评论:

  1. Are you actually coming from somewhere? If you refresh your browser this value will likely not be sent. So make sure your browser is sending the header. If you put this script on a public url, I'll be happy to check it out and verify.
  2. You should really turn on all errors. If the header is not sent and you access it anyway, PHP will emit an E_NOTICE. If you're debugging your code you should turn on all error message and make sure there are no E_NOTICE's or worse.
  1. 你真的来自某个地方吗?如果您刷新浏览器,则可能不会发送此值。因此,请确保您的浏览器正在发送标头。如果您将此脚本放在公共网址上,我很乐意查看并验证。
  2. 你真的应该打开所有错误。如果标头没有发送并且您仍然访问它,PHP 将发出一个 E_NOTICE。如果您正在调试代码,您应该打开所有错误消息并确保没有 E_NOTICE 或更糟。

回答by Simon

Maybe a stupid remark, but $_SERVER["HTTP_REFERER"]only works if you enter the page using a hyperlink. e.g.

也许是一句愚蠢的话,但$_SERVER["HTTP_REFERER"]只有在您使用超链接进入页面时才有效。例如

/goto.html

/goto.html

<a href="refer.php">go to refer</a>

/refer.php

/refer.php

<?php
print "You entered using a link on ".$_SERVER["HTTP_REFERER"];
?>

HTTP_REFERER doesn't work if you enter the link location directly in your browser.

如果您直接在浏览器中输入链接位置,则 HTTP_REFERER 不起作用。

回答by Ignacio Vazquez-Abrams

getenv()is used if it's being run as a CGI script. With a SAPI you use $_SERVER["HTTP_REFERER"].

getenv()如果它作为 CGI 脚本运行,则使用它。通过 SAPI,您可以使用$_SERVER["HTTP_REFERER"].

回答by Lan Tait

<?php
  echo $_SERVER['HTTP_REFERER'];
?>

The above code works! However, many of my students find it hard, at first, to grasp that $_SERVER['HTTP_REFERER']requiresarriving from a link.

上面的代码有效!然而,我的许多学生一开始很难理解$_SERVER['HTTP_REFERER']需要链接到达。

I give them the below (tested)code (or "web page")to demonstrate. The above code is at the bottom.

我给他们以下(经过测试的)代码(或“网页”)来演示。上面的代码在底部。

show-referer.php

显示-referer.php

<?php
  if ( isset( $_SERVER['HTTP_REFERER'] ) ) {
    $referer = $_SERVER['HTTP_REFERER'];
  } else {
    $referer = 'No Link - No Referer - Direct URL Entry';
  }
  echo $referer;
?>

<p>See the referer in action
  <a href="show-referer.php">from this page!</a>
</p>

<?php
  echo $_SERVER['HTTP_REFERER'];
?>

The show-referer.php page links to itself when you click the link, which should cause the browser to generate an HTTP_REFERER.

当您单击链接时,show-referer.php 页面会链接到自身,这会导致浏览器生成 HTTP_REFERER。

回答by Emil Vikstr?m

$ref = $_SERVER['HTTP_REFERER'];

Relevant manual page: http://php.net/manual/en/reserved.variables.server.php

相关手册页:http: //php.net/manual/en/reserved.variables.server.php

回答by Farid

If you compute all these answers, you end up with something looking like :

如果你计算所有这些答案,你最终会得到如下所示的结果:

<?php
if isset($_SERVER['HTTP_REFERER']) {
    $ref = $_SERVER['HTTP_REFERER'];
}
else {
    $ref = "Direct Entry";
}
?>

回答by f13o

Again, read http://php.net/manual/en/reserved.variables.server.php: With HTTP_REFERER there is a comment:

再次阅读http://php.net/manual/en/reserved.variables.server.php:使用 HTTP_REFERER 有一条评论:

The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.

将用户代理引用到当前页面的页面地址(如果有)。这是由用户代理设置的。并不是所有的用户代理都会设置这个,有些提供修改 HTTP_REFERER 的能力作为一个特性。简而言之,它不能真正被信任