简体   繁体   English

如何在PHP中检查访问者IP地址的代码功能?

[英]How to code feature on checking visitor IP address in PHP?

I still confused on this subject and ask for any help and reference on how to check a visitor IP address via PHP. 我仍然对此主题感到困惑,并请求有关如何通过PHP检查访客IP地址的任何帮助和参考。

I'm know it seems lazy to ask something before attempting to code something to show, but right now, I am also googling to find it. 我知道在尝试编写代码之前先问一些东西似乎很懒,但是现在,我也在谷歌上搜索它。 Hopefully someone can give general broad answer or some link to read? 希望有人可以给出一般的广泛答案或一些阅读链接?

BTW, what is to be considered when we have to code some feature like this? 顺便说一句,当我们必须编写这样的一些功能时需要考虑什么?

PS: Thank you very much everybody. PS:非常感谢大家。 It's been enlightening arguments. 这是有启发性的论点。 Frankly, I choose the answer more as respect rather than the truth in it. 坦率地说,我更多地选择答案作为尊重而不是真理。 Because until now I still don't know the right one. 因为到现在为止我还是不知道对的。 Maybe I need more years of learning before I get a firm understanding of the topic itself. 也许在我对这个话题本身有一个坚定的理解之前,我需要更多年的学习。

请参阅$ _SERVER ,具体如下:

$_SERVER['REMOTE_ADDR'];

OK, justjoe, I see you got confused with this arguement and there is my big part in it. 好的,justjoe,我看到你对这个争论感到困惑,我的重要部分就在其中。

Some more explanations for you. 还有一些解释。

The answer depends on the task. 答案取决于任务。 You have 2 options: 你有2个选择:

If you need only one IP address, you can use only REMOTE_ADDR and nothing else. 如果只需要一个IP地址,则只能使用REMOTE_ADDR而不能使用其他任何IP地址。 Take a look at the web-server's access log: there is only one IP address and it's REMOTE_ADDR one. 看看Web服务器的访问日志:只有一个IP地址,它是REMOTE_ADDR。 At least it guaranteed you a valid IP address. 至少它保证你有效的IP地址。 In many cases, like a traffic counter, it's the only thing you can rely on. 在许多情况下,就像交通计数器一样,这是你唯一可以信赖的东西。 Thes is general answer to the "How to get an IP address" question. 这是对“如何获取IP地址”问题的一般回答。

If you want to record an address that can be more precise probably - so, no one forbids you from recording many addresses, not one. 如果你想记录一个可能更精确的地址 - 那么,没有人禁止你录制许多地址,而不是一个。 But of course, you have to record these HTTP headers along with REMOTE_ADDR, not instead of it. 但是,当然,您必须记录这些HTTP标头以及REMOTE_ADDR,而不是它。 There is some use for such a throng of addresses. 这样一大堆地址有一些用处。 But you can't rely on it too much. 但你不能过分依赖它。 But you can dig some information from it, if you care. 但是如果你关心的话,你可以从中挖掘一些信息。

The only case for the FORWARDED_FOR header is a misconfigured webserver, who place the real IP address into this variable. FORWARDED_FOR标头的唯一情况是配置错误的网络服务器,它将真实IP地址放入此变量中。 In this case it can be used as an IP address. 在这种情况下,它可以用作IP地址。 But of course it must be done manually, in the every particular case, not as the general recommendation. 但是,当然必须在每种特定情况下手动完成,而不是作为一般建议。 But anyway I'd quit such a webserver as there can't only be one misconfiguration in it. 但无论如何,我会退出这样一个网络服务器,因为它不仅可能存在一个错误配置。

要获取用户IP地址,您应该使用此地址,

$ip = $_SERVER['REMOTE_ADDR']

If your client is surfing through a proxy server, then $_SERVER['REMOTE_ADDR'] just returns the IP address of the proxy server — not of the client's machine. 如果您的客户端正在浏览代理服务器,那么$_SERVER['REMOTE_ADDR']只返回代理服务器的IP地址 - 而不是客户机的IP地址。 That's not very reliable. 那不太可靠。 This might be a better solution: 这可能是一个更好的解决方案:

function get_ip_address() {
 foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key) {
  if (array_key_exists($key, $_SERVER)) {
   foreach (explode(',', $_SERVER[$key]) as $ip) {
    $ip = trim($ip); // just to be safe
    if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false) {
     self::$ip = $ip;
     return $ip;
    }
   }
  }
 }
}

This has already been discussed on Stack Overflow before. 这已经在Stack Overflow上讨论过了。 Please refer to “What is the most accurate way to retrieve a user's correct IP address in PHP?” . 请参阅“在PHP中检索用户正确IP地址的最准确方法是什么?” The above code is an optimized version of the accepted answer to that question. 上面的代码是该问题的已接受答案的优化版本。

However, do note that getting IP addresses is never fully reliable: php / ajax REMOTE_ADDR set to IP of bogus network adapter 但请注意,获取IP地址永远不可靠: php / ajax REMOTE_ADDR设置为伪网络适配器的IP

General broad answer: everything PHP knows about client is stored in the $_SERVER variable. 一般的答案:PHP了解客户端的所有内容都存储在$ _SERVER变量中。 So, do this code everytime you want particular info to see if you can find something relevant: 因此,每当您想要特定信息时,请查看是否可以找到相关内容:

echo "<pre>";
print_r($_SERVER);
echo "</pre>";
//or just
phpinfo(32);

$_SERVER['REMOTE_ADDR'] is the only IP address you can get, though it can be not a "client address". $ _SERVER ['REMOTE_ADDR']是唯一可以获得的IP地址 ,但它可能不是“客户端地址”。

The following would serve the purpose. 以下内容可达到此目的。

$ip = $_SERVER['REMOTE_ADDR'];

echo "IP address is : " :.$ip;

I suggest you use a third-party IP address to a geo converter to calculate the location. 我建议您使用第三方IP地址到地理转换器来计算位置。 Since you wouldn't have to bare the database space.. Accuracy and the queries. 既然你不必裸露数据库空间..准确性和查询。

All you will need to do is something like 您需要做的就是这样

http://third-party-url.com?ip=IP_ADDRESS

and it gives back XML or an array of data. 它会返回XML或数据数组。

You can use that to do whatever you want later on. 你可以用它来做你想做的事情。

Here are two websites which are popular. 这是两个受欢迎的网站。 * My IP Address Lookup and GeoTargeting * SimpleGeo * 我的IP地址查询和GeoTargeting * SimpleGeo

There are many such sites.. Maybe others can add them here. 有很多这样的网站..也许其他人可以在这里添加它们。

Just use this at the top of your code: 只需在代码顶部使用它:

if (function_exists('apache_request_headers'))
{
    $headers = apache_request_headers();

    if (array_key_exists('X-Forwarded-For', $headers))
    {
        $ips = explode(',',$headers['X-Forwarded-For'],2);
        $_SERVER["REMOTE_ADDR"]= trim($ips[0]);
    }
}

It will make sure that $_SERVER["REMOTE_ADDR"] always keep the proper IP address on load balancers applications or not. 它将确保$_SERVER["REMOTE_ADDR"]始终在负载均衡器应用程序上保留正确的IP地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM