Question: I'm trying to get the country from which the user is browsing the website so I can work out what currency to show on the website. I have tried using the GET scripts available from: <http://api.hostip.info> but they just return XX when I test it.

If anyone knows any better methods please share.

Thanks.

Answer: 
I use this:

```
  $_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
  $ip = $_SESSION['ip'];
  $try1 = "http://ipinfodb.com/ip_query.php?ip=".$ip."&output=xml";
  $try2 = "http://backup.ipinfodb.com/ip_query.php?ip=".$ip."&output=xml";
  $XML = @simplexml_load_file($try1,NULL,TRUE);
  if(!$XML) { $XML = @simplexml_load_file($try2,NULL,TRUE); }
  if(!$XML) { return false; }

  //Retrieve location, set time
  if($XML->City=="") { $loc = "Localhost / Unknown"; }
  else { $loc = $XML->City.", ".$XML->RegionName.", ".$XML->CountryName; }
  $_SESSION['loc'] = $loc;

```
There are many ways to do it as suggested by those earlier. But I suggest you take a look at the IP2 PHP library available at <https://github.com/ip2iq/ip2-lib-php> which we developed.

You can use it like below:

```
<?php
    require_once("Ip2.php");

    $ip2 = new \ip2iq\Ip2();
    $country_code = $ip2->country('8.8.8.8');
    //$country_code === 'US'
?>

```

It doesn't need any SQL or web service lookup just a local data file. It is faster than almost all other methods out there. The database is updated monthly you can download it for free.

The only thing you will need to do for now if you need the country name in your language is map it to an associative array from something like this <https://gist.github.com/DHS/1340150>