views:

39

answers:

4

Hello,

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.

A: 

You should use the geoip library.

Maxmind provides free databases and commercial databases, with a difference in the date of last update and precision, the commercial being of better quality.

See http://www.maxmind.com/app/geolitecountry for the free database.

I think it should be sufficient for basic needs.

SirDarius
A: 

You can use Geolocation to get the Coordinates and then some Service to get the Country from that, but the geolocation API is browser based so you can only access it via JavaScript and then have to pass theese Informations to PHP somehow, i wrote something on the JS Part once:

http://www.lautr.com/utilizing-html5-geolocation-api-and-yahoo-placefinder-example

When it comes to getting the Location via the IP, there are a bazillion Services out there who offer databases for that, some free, some for charge, some with a lot of IP's stored and much data, some with less, for example the one you mentioned, works just fine:

http://api.hostip.info/?ip=192.0.32.10

So You can ether go with the Geolocation API which is pretty neat, but requires the users permission, works via JS and doesnt work in IE (so far) or have to look for a IPÜ Location Service that fits your needs :)

Hannes
+3  A: 

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;
Steve
This makes a request every time it is run (no local caching of already resolved IPs). If the external web site is down, it will take the page 30 or 60 seconds to load
Pekka
What you're describing would require an offline cache of IPs, which defeats the purpose of the exercise as I understand it.
Steve
+1  A: 

Try these:

http://ip-to-country.webhosting.info/

http://www.ip2location.com/

Both are IP address-to-country databases, which allow you to look up the country of origin of a given IP address.

However it's important to note that these databases are not 100% accurate. They're a good guide, but you will get false results for a variety of reasons.

  • Many people use proxying to get around country-specific blocks and filters.
  • Many IP ranges are assigned to companies with large geographic spread; you'll just get the country where they're based, not where the actual machine is (this always used to be a big problem for tracking AOL users, because they were all apparently living in Virginia)
  • Control of IP ranges are sometimes transferred between countries, so you may get false results from that (especially for smaller/less well-connected countries)

Keeping your database up-to-date will mitigate some of these issues, but won't resolve them entirely (especially the proxying issue), so you should always allow for the fact that you will get false results.

Spudley