简体   繁体   English

获取用户计算机的正确IP地址

[英]Get correct IP address of user machine

When I check ip from ipconfig on local host. 当我从本地主机上的ipconfig检查ip时。 I am getting ip like this 100.78.80.xxx (US) , when I check IP on some website like ip2location/ip2nation , I am getting IP like this 115.245.20.xxx (IN) . 我正在像这样100.78.80.xxx (US)获取IP,当我在某些网站上检查IP如ip2location/ip2nation ,我得到的IP就像这样115.245.20.xxx (IN) I want to know why? 我想知道为什么?

I want to get the user IP address to get the country name. 我想获取用户IP地址以获取国家/地区名称。 I am using $_SERVER['REMOTE_ADDR'] to get the user IP address and will get the country name. 我使用$_SERVER['REMOTE_ADDR']获取用户IP地址并获取国家/地区名称。

Does it get the correct IP of user machine to get the correct country code? 是否获得正确的用户计算机IP以获取正确的国家/地区代码?

它获取用户网关或路由器的IP地址,当用户使用路由器时,用户的IP地址可能会有所不同,但是这个IP足以获得该县,它将为您提供正确的国家/地区。

Luthando Loot is right. Luthando Loot是对的。 The answer is present here: Getting visitors country from their IP 答案就在这里: 让访客来自他们的知识产权

However, it is located in answer but not in question. 但是,它位于答案但不是问题。

if (isset($_SERVER['HTTP_CLIENT_IP']))
{
    $real_ip_adress = $_SERVER['HTTP_CLIENT_IP'];
}

if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
{
    $real_ip_adress = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
    $real_ip_adress = $_SERVER['REMOTE_ADDR'];
}

You should check this values in such order: 您应该按以下顺序检查此值:

HTTP_CLIENT_IP
HTTP_X_FORWARDED_FOR
REMOTE_ADDR

It doesn't mean that you will precisely get his IP address. 这并不意味着您将准确获取其IP地址。 It can be easily hidden by anonymizer, proxy, ISP etc. However, it is much more efficient than loooking at REMOTE_ADDR only. 它可以很容易地被匿名者,代理,ISP等隐藏。但是,它比仅仅使用REMOTE_ADDR更有效。

There are some other values in SERVER array. SERVER数组中还有一些其他值。 Actually, this question is a duplicate to this: 实际上,这个问题与此重复:
What is the most accurate way to retrieve a user's correct IP address in PHP? 在PHP中检索用户正确IP地址的最准确方法是什么?

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

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