简体   繁体   English

如何使用php获取访问者的IP地址

[英]How to use php to get ip address of a visitor

i want to get the location of my users, but i don't want to use third party systems to do that我想获得我的用户的位置,但我不想使用第三方系统来做到这一点
So i made this script所以我做了这个脚本

    <?php
 function getIPAddress() {  
    //whether ip is from the share internet  
     if(!empty($_SERVER['HTTP_CLIENT_IP'])) {  
                $ip = $_SERVER['HTTP_CLIENT_IP'];  
        }  
    //whether ip is from the proxy  
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {  
                $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];  
     }  
//whether ip is from the remote address  
    else{  
             $ip = $_SERVER['REMOTE_ADDR'];  
     }  
     return $ip;  
}  
$ip = getIPAddress();  
echo $ip; 
?>

when i go to http://localhost it echo's ::1 then when i connect to the internet and i go to http://192.168.43.104 it echo's 192.168.43.104 and i saw online that 192.168.xxx.xxx is a public ip and that there are millions of users using 192.168.xxx.xxx ,and that my visitors location cannot be gotten from that当我访问 http://localhost 时它是 ::1 然后当我连接到互联网时我去http://192.168.43.104它是 192.168.43.104 我在网上看到 192.168.xxx.xxx 是公共的ip 并且有数百万用户使用 192.168.xxx.xxx ,并且无法从中获取我的访客位置
My questions are我的问题是

  1. is it true that 192.168.xxx.xxx cannot be used to determine a users location 192.168.xxx.xxx 不能用于确定用户位置是真的吗
  2. if the above script cannot get users location then how can i do that如果上述脚本无法获取用户位置,那么我该怎么做
    Just to be clear i just want their IP addresses because i would need a third party service to change their ip address to a location, so i think it would be better to leave it an numbers为了清楚起见,我只想要他们的IP地址,因为我需要第三方服务来将他们的 IP 地址更改为某个位置,所以我认为最好留下一个数字

EDIT编辑
on my page currently it tells me 192.168.43.104 but if i google 'my ip', i get 197.210.85.238 Your public IP address is it from my public ipaddress that my location can be gotten from目前在我的页面上它告诉我 192.168.43.104 但如果我用谷歌搜索“我的 ip”,我会得到197.210.85.238 Your public IP address是从我的公共197.210.85.238 Your public IP address中获得的,我的位置可以从

Your server can only learn your public IP when you are accessing it through WAN (aka the public internet).您的服务器只能在您通过 WAN(又名公共互联网)访问时了解您的公共 IP。

If you are accessing your server with IP like these:如果您使用以下 IP 访问服务器:

  • 10.0.0.0 – 10.255.255.255 10.0.0.0 – 10.255.255.255
  • 172.16.0.0 – 172.31.255.255 172.16.0.0 – 172.31.255.255
  • 192.168.0.0 – 192.168.255.255 192.168.0.0 – 192.168.255.255

please notice that they are IANA reserves IPv4 addresses for private networks , which are supposed to be only used behind routers.请注意,它们是 IANA 为私有网络保留的 IPv4 地址,应该只在路由器后面使用。 And any routers are free to use them (thus it has no meaning in locating the user geolocation).并且任何路由器都可以自由使用它们(因此它对定位用户地理位置没有意义)。

Since you are only accessing that server through local area Network (LAN), your traffic has not route through the public internet at all.由于您仅通过局域网 (LAN) 访问该服务器,因此您的流量根本没有通过公共互联网路由。 The local network traffic would contains no public IP information, which you was asking for.本地网络流量将不包含您要求的公共 IP 信息。

In short, please put your application on a public server to properly test your code .简而言之,请将您的应用程序放在公共服务器上以正确测试您的代码

PS As others has pointed out, geoip data is never 100% accurate. PS 正如其他人指出的那样,geoip 数据从来都不是 100% 准确的。 You need to accept your result to be inaccurate if you decide to determine users location solely based on IP.如果您决定仅根据 IP 确定用户位置,则您需要接受结果不准确。

Let me answer your two questions.我来回答你的两个问题。

  1. Is it true that 192.168.xxx.xxx cannot be used to determine a users location 192.168.xxx.xxx 不能用于确定用户位置是真的吗

Yes, it is a private IP address range.是的,它是一个私有 IP 地址范围。 IP geolocation service, such as IP2Location, is working on the public IP address ranges only. IP 地理定位服务(例如 IP2Location)仅适用于公共 IP 地址范围。

  1. If the above script cannot get users location then how can i do that.如果上述脚本无法获取用户位置,那么我该怎么做。 Just to be clear i just want their IP addresses because i would need a third party service to change their ip address to a location, so i think it would be better to leave it an numbers为了清楚起见,我只想要他们的 IP 地址,因为我需要第三方服务来将他们的 IP 地址更改为某个位置,所以我认为最好留下一个数字

The issue is not your script.问题不在于您的脚本。 A simple $_SERVER['REMOTE_ADDR'];一个简单的 $_SERVER['REMOTE_ADDR']; should get your visitor IP address in most cases.在大多数情况下,应该获取您的访问者 IP 地址。 However, you need to test it in WAN and not LAN.但是,您需要在 WAN 而不是 LAN 中对其进行测试。 For example, you put the page in a server located in data center and access it from another network at office.例如,您将页面放在位于数据中心的服务器中,并从办公室的另一个网络访问它。

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

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