Does anyone know of a simple way to retrieve the country for a given IP Address? Preferably in ISO_3166-1 format?
Here's a nice free service with a public API: http://www.hostip.info/use.html
A lot of people (including my company) seem to use MaxMind GeoIP.
They have a free version GeoLite which is not as accurate as the paid version, but if you're just after something simple, it may be good enough.
I don't know how accurate that hostip.info site is. I just visted that site, and it reported that my country is Canada. I'm in the US and the ISP that my office uses only operates from the US. It does allow you to correct it's guess, but if you are using this service to track web site vistors by country, you'll have no way of knowing if the data is correct. Of course, I'm just one data point. I downloaded the GeoLite Country database, which is just a .csv file, and my IP address was correctly identified as US.
Another benefit of the MaxMind product line (paid or free) is that you have the data, you don't incur the performance hit of making a web service call to another system.
@Edo, so you clicked on the "Make a correction" link and gave them a clue, right? If you tell them, they'll be able to help other users in your area.
There are two approaches: using an Internet service and using some kind of local list (perhaps wrapped in a library). What you want will depend on what you are building.
For services:
- http://www.hostip.info/use.html (as mentioned by Mark)
- http://www.team-cymru.org/Services/ip-to-asn.html
For lists:
- http://www.maxmind.com/app/geoip_country (as mentioned by Orion)
You could roll your own by downloading the lists from the RIRs:
- ftp.arin.net/pub/stats/arin/delegated-arin-latest
- ftp.ripe.net/ripe/stats/delegated-ripencc-latest
- ftp.afrinic.net/pub/stats/afrinic/delegated-afrinic-latest
- ftp.apnic.net/pub/stats/apnic/delegated-apnic-latest
ftp.lacnic.net/pub/stats/lacnic/delegated-lacnic-latest
The format is documented in this README
The most accurate is Digital Elements NetAcuity...not free but you get what you pay for most of the time....Digital Element
ipinfodb provide a free database and API for ip to country and vice versa. They use free data from MaxMind. The data got updated every month, and it's a great free alternative with decent accuracy.
google's clientlocation returns (my example)
latlng = new google.maps.LatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude);
location = "IP location: " + getFormattedLocation();
document.getElementById("location").innerHTML = location;