-1

Possible Duplicate:
IP to Country?

I am trying to get the country name with php by using

$country = file_get_contents("http://api.hostip.info/country.php?ip=".$_SERVER['REMOTE_ADDR']);

it is working fine for me (returns US). But when I used it on my website, for most of my users it is returning XX and that is not a country code. Is there any way I can get the country name with php ? PLEASE HELP

Community
  • 1
  • 1
user1481850
  • 238
  • 3
  • 13
  • 24
  • FYI: not all IPs have geo mappings, or the api provider's restricting access to that country's data to for-pay service tiers. – Marc B Sep 14 '12 at 19:57
  • What does $_SERVER['REMOTE_ADDR'] contain? Maybe that global variable isn't populating correctly due to some server configuration? For example, if I go to http://api.hostip.info/country.php?ip=0.0.0.0, I also get 'XX'. – AndrewR Sep 14 '12 at 19:58
  • possible dublicate of http://stackoverflow.com/questions/1736024/php-accessing-the-users-country-locale – Krycke Sep 14 '12 at 20:00

1 Answers1

3

Use mod_geoip, then you'll have access to the country name in $_SERVER['GEOIP_COUNTRY_NAME']

Tchoupi
  • 14,560
  • 5
  • 37
  • 71
  • 1
    Good idea. Also, there is a PECL library that connects to the same system as mod_geoip and adds new PHP functions to make calls to the country database. [Download](http://pecl.php.net/package/geoip) / [Documentation](http://www.php.net/manual/en/ref.geoip.php) – AndrewR Sep 14 '12 at 20:06