简体   繁体   中英

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
My questions are

  1. is it true that 192.168.xxx.xxx cannot be used to determine a users location
  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

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

Your server can only learn your public IP when you are accessing it through WAN (aka the public internet).

If you are accessing your server with IP like these:

  • 10.0.0.0 – 10.255.255.255
  • 172.16.0.0 – 172.31.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. 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. The local network traffic would contains no public IP information, which you was asking for.

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. You need to accept your result to be inaccurate if you decide to determine users location solely based on IP.

Let me answer your two questions.

  1. Is it true that 192.168.xxx.xxx cannot be used to determine a users location

Yes, it is a private IP address range. IP geolocation service, such as IP2Location, is working on the public IP address ranges only.

  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

The issue is not your script. A simple $_SERVER['REMOTE_ADDR']; should get your visitor IP address in most cases. However, you need to test it in WAN and not LAN. For example, you put the page in a server located in data center and access it from another network at office.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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