How to get the Country according to a certain IP?

Viewed 18530

Does anyone know any simple way to retrieve the country from a given IP Address, preferably in ISO_3166-1 format?

15 Answers

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:

For lists:

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.

ipinfodb provides a free database and API for IP to country and vice versa. They use free data from MaxMind. The data gets updated every month, and it's a great free alternative with decent accuracy.

I don't know how accurate http://hostip.info site is. I just visited 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 its guess, but if you are using this service to track web site visitors 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.

The most accurate is Digital Elements NetAcuity. It's not free but you get what you pay for most of the time.

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;

You can give a try to https://astroip.co, it is a new Geolocation API I built which exposes geo data together with other useful datapoints like currency, timezone, ASN data and security.

Here it is an example of the json response:

curl https://api.astroip.co/70.163.7.1
{
  "status_code": 200,
  "geo": {
    "is_metric": false,
    "is_eu": false,
    "longitude": -77.0924,
    "latitude": 38.7591,
    "country_geo_id": 6252001,
    "zip_code": "22306",
    "city": "Alexandria",
    "region_code": "VA",
    "region_name": "Virginia",
    "continent_code": "NA",
    "continent_name": "North America",
    "capital": "Washington",
    "country_name": "United States",
    "country_code": "US"
  },
  "asn": {
    "route": "70.160.0.0/14",
    "type": "isp",
    "domain": "cox.net",
    "organization": "ASN-CXA-ALL-CCI-22773-RDC",
    "asn": "AS22773"
  },
  "currency": {
    "native_name": "US Dollar",
    "code": "USD",
    "name": "US Dollar",
    "symbol": "$"
  },
  "timezone": {
    "is_dst": false,
    "gmt_offset": -18000,
    "date_time": "2020-12-05T17:04:48-05:00",
    "microsoft_name": "Eastern Standard Time",
    "iana_name": "America/New_York"
  },
  "security": {
    "is_crawler": false,
    "is_proxy": false,
    "is_tor": false,
    "tor_insights": null,
    "proxy_insights": null,
    "crawler_insights": null
  },
  "error": null,
  "ip_type": "ipv4",
  "ip": "70.163.7.1"
}
Related