The user's browser is configured to block the referrer header (rare)
The user is switching protocols (i.e. a link on a http site pointing to a https one, or vice versa)
In those cases, there is nothing you can do.
If you control the linking site however, you could add a referrer ID in the GET parameter to the link:
http://example.com/?from=mysite
you could then parse the from parameter in your script.
Converting the referrer string to an IP usually not a good idea, seeing as many IP addresses host dozens or hundreds of sites. The distinction which site the user came from will be lost that way.
The referring address is present in the request header from all cooperating browsers for which there is a referrer. If the user just typed in your URL, then there is no referrer.
It is a trivial programming matter to convert the URL into an IP address:
If HTTP_REFERER does not show up in server, then that user did not enter your site by a link, or he has a way to mask that variable via his browser. Not much else you can do.
If it does not show up in $_SERVER it usually means the client is not sending it. The referrer is data that cannot be trusted for accuracy as it is entirely up to the user (more specifically their browser).
Your best bet is to check if the referer is there, then use the gethostbyname() function on the domain to get the IP you want.
发布评论
评论(5)
HTTP_REFERER
标头必须由客户端浏览器发送。您不能依赖它被发送。未发送的情况包括:
在这些情况下,您无能为力。
但是,如果您控制链接站点,则可以在链接的 GET 参数中添加引用者 ID:
然后可以在脚本中解析
from
参数。将引荐来源网址字符串转换为 IP 通常不是一个好主意,因为许多 IP 地址托管着数十个或数百个站点。这样一来,用户来自哪个网站的区别就会消失。
The
HTTP_REFERER
header has to be sent by the client's browser. You can't rely on it being sent.Scenarios when it does not get sent include:
In those cases, there is nothing you can do.
If you control the linking site however, you could add a referrer ID in the GET parameter to the link:
you could then parse the
from
parameter in your script.Converting the referrer string to an IP usually not a good idea, seeing as many IP addresses host dozens or hundreds of sites. The distinction which site the user came from will be lost that way.
引用地址存在于来自所有具有引用者的协作浏览器的请求标头中。如果用户只是输入您的 URL,则没有引荐来源网址。
将 URL 转换为 IP 地址是一个简单的编程问题:
The referring address is present in the request header from all cooperating browsers for which there is a referrer. If the user just typed in your URL, then there is no referrer.
It is a trivial programming matter to convert the URL into an IP address:
如果 HTTP_REFERER 没有出现在服务器中,则该用户没有通过链接进入您的网站,或者他有办法通过浏览器屏蔽该变量。您无能为力。
If HTTP_REFERER does not show up in server, then that user did not enter your site by a link, or he has a way to mask that variable via his browser. Not much else you can do.
如果它没有出现在 $_SERVER 中,通常意味着客户端没有发送它。引荐来源网址是无法信任其准确性的数据,因为它完全取决于用户(更具体地说是他们的浏览器)。
最好的办法是检查引用者是否存在,然后在域上使用 gethostbyname() 函数来获取所需的 IP。
http://php.net/manual/en/function.gethostbyname.php
If it does not show up in $_SERVER it usually means the client is not sending it. The referrer is data that cannot be trusted for accuracy as it is entirely up to the user (more specifically their browser).
Your best bet is to check if the referer is there, then use the gethostbyname() function on the domain to get the IP you want.
http://php.net/manual/en/function.gethostbyname.php
您可以使用
$_SERVER['HTTP_REFERER']
确定引用 URL,但请记住这可以被操纵。然后,您可以使用
gethostbyname($referrer)
获取 IP 地址。请参阅: http://php.net/manual/en/reserved.variables.server .php 和 http://php.net/manual/en/function。 gethostbyname.php
You can determine the reffering URL with
$_SERVER['HTTP_REFERER']
but bear in mind this can be manipulated.You can then use
gethostbyname($referrer)
to get the IP address.See: http://php.net/manual/en/reserved.variables.server.php and http://php.net/manual/en/function.gethostbyname.php