-2

I want to retrieve information like the city, state, and country of a visitor from their IP address, so that I can customize my web page according to their location. Is there a good and reliable way to do this in PHP? without java script i am using this code but result is null for city

$user_ip = getenv('REMOTE_ADDR');
$geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$user_ip"));
$city = $geo["geoplugin_city"];
$region = $geo["geoplugin_regionName"];
$country = $geo["geoplugin_countryName"];

echo "City: ".$city."<br>";
echo "Region: ".$region."<br>";
echo "Country: ".$country."<br>";

result for the city not work many thanks

Morne
  • 1,623
  • 2
  • 18
  • 33
  • You can't get these informations precisely only with user IP. You should look to other (more elaborated) methods. – Michel Sep 16 '15 at 09:35
  • whats this methods we needs thanks – user2738924 Sep 16 '15 at 09:39
  • I just tryied with a bunch of IP adress, mine for example didn't return any `city`, but using the IP adress **8.8.8.8** city is returned (as _Mountain View_). So I think some IP adress have enabled a configuration to protect against location tracking or something that block the localization information. – Anwar Sep 16 '15 at 09:40
  • 2
    possible duplicate of [get region/city from ip](http://stackoverflow.com/questions/6350626/get-region-city-from-ip) – YvesHendseth Sep 16 '15 at 10:14
  • @Zeratops Or, the less sinister explanation: **the city is simply not known.** Geolocation by IP isn't exactly an exact science. – deceze Sep 16 '15 at 10:26

1 Answers1

0

Geolocation-by-IP works by accumulating a database that maps IPs to geographic locations. The source of this data is varied, often it's an accumulation of public IP allocation records. As a bit of background, there are institutions which regulate the assignment of IP addresses. There's a global body assigning ranges of IP addresses to smaller regional bodies, which further pass those on to more local organisations like ISPs or hosting companies, which ultimately dole them out to their customers.

If a large ISP, say, receives a range of IP addresses, then this may be public knowledge. However, if this ISP is operating trans-regionally, then one customer may get an address today and a completely different customer in a completely different city may get it tomorrow. It is simply not known whom this IP is assigned to at any one time. The vague geographic region the ISP operates in is known, but which specific customer in which specific city currently has said IP address is not.

deceze
  • 510,633
  • 85
  • 743
  • 889