确定引用站点的 IP 地址

发布于 2024-11-11 13:36:14 字数 1455 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

走野 2024-11-18 13:36:14

HTTP_REFERER 标头必须由客户端浏览器发送。您不能依赖它被发送。

未发送的情况包括:

  • 用户手动输入地址
  • 用户在大型电子邮件客户端之一中打开一个链接,该客户端 花费大量精力来掩盖 REFERER
  • 用户的浏览器被配置为阻止引用标头(罕见)
  • 用户正在切换协议(即 http 站点上的链接指向 https 站点,反之亦然)

在这些情况下,您无能为力。

但是,如果您控制链接站点,则可以在链接的 GET 参数中添加引用者 ID:

http://example.com/?from=mysite

然后可以在脚本中解析 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:

  • The user enters the address by hand
  • The user opens a link in one of the big E-Mail clients who go through great lengths to obscure the REFERER
  • 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.

娇纵 2024-11-18 13:36:14

引用地址存在于来自所有具有引用者的协作浏览器的请求标头中。如果用户只是输入您的 URL,则没有引荐来源网址。

将 URL 转换为 IP 地址是一个简单的编程问题:

<?php
$raddr = gethostbyaddr($_SERVER['HTTP_REFERER']);

?>

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:

<?php
$raddr = gethostbyaddr($_SERVER['HTTP_REFERER']);

?>
扭转时空 2024-11-18 13:36:14

如果 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.

羞稚 2024-11-18 13:36:14

如果它没有出现在 $_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

蓝眸 2024-11-18 13:36:14

您可以使用 $_SERVER['HTTP_REFERER'] 确定引用 URL,但请记住这可以被操纵。

然后,您可以使用 gethostbyname($referrer) 获取 IP 地址。

请参阅: http://php.net/manual/en/reserved.variables.server .phphttp://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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文