61

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

Tanjim Ahmed Khan
  • 650
  • 1
  • 9
  • 21
DylanJ
  • 2,373
  • 3
  • 25
  • 24
  • 8
    Don't place too much reliance on the IP telling you the country where the user is sitting: because I work for a Scandinavian company I keep seeing adverts in Swedish, even though our office is in Scotland... – AAT Jan 13 '10 at 13:45
  • Exactly - if you think about it. There are many cases where you may appear to come from somewhere, but actually come from somewhere else. Its all down to the fact that network topology does not have to confirm to political or geological borders. F.ex. VPN's, wireless, satelite and so on. – Phluks Feb 06 '13 at 10:26

15 Answers15

42

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:

Community
  • 1
  • 1
Warren Blanchet
  • 1,201
  • 1
  • 12
  • 13
  • 1
    One of the most useful answers in general that I have seen in SO. Thank you! – tzot Oct 03 '08 at 12:08
  • Do you maybe know if the RIRs also have more detailed information, like the geopipcity db from maxmind, available as open data? I did look, but could not find it – Jevado Jul 29 '11 at 09:44
  • Just in case... https://ftp.arin.net/pub/stats/arin/delegated-arin-extended-latest – fcm Jul 29 '23 at 15:29
38

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.

Orion Edwards
  • 121,657
  • 64
  • 239
  • 328
11

Here's a nice free service with a public API: http://www.hostip.info/use.html

Mark Harrison
  • 297,451
  • 125
  • 333
  • 465
9

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.

Brad Koch
  • 19,267
  • 19
  • 110
  • 137
Donny Kurnia
  • 5,260
  • 5
  • 35
  • 52
  • Way better than maintaining your own database. Just tested it on a really obscure place (Uthai Thani, Thailand) and it was spot on. Great find, thanks! – Jannie Theunissen Jul 29 '10 at 13:05
6

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.

Tanjim Ahmed Khan
  • 650
  • 1
  • 9
  • 21
Chris Miller
  • 4,809
  • 4
  • 33
  • 50
3

Try this php code

  <?php  $ip = $_SERVER['REMOTE_ADDR'];
    $json = file_get_contents("http://api.easyjquery.com/ips/?ip=".$ip."&full=true");
    $json = json_decode($json,true);
    $timezone = $json[localTimeZone];?>
pckabeer
  • 686
  • 6
  • 27
3

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

Tanjim Ahmed Khan
  • 650
  • 1
  • 9
  • 21
CSharpAtl
  • 7,374
  • 8
  • 39
  • 53
3

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;
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
3

You can use my service, http://ipinfo.io, for this. The API returns a whole bunch of different details about an IP address:

$ curl ipinfo.io/8.8.8.8
{
  "ip": "8.8.8.8",
  "hostname": "google-public-dns-a.google.com",
  "loc": "37.385999999999996,-122.0838",
  "org": "AS15169 Google Inc.",
  "city": "Mountain View",
  "region": "CA",
  "country": "US",
  "phone": 650
}

If you're only after the country code you just need to add /country to the URL:

$ curl ipinfo.io/8.8.8.8/country
US

Here's a generic PHP function you could use:

function ip_details($ip) {
    $json = file_get_contents("http://ipinfo.io/{$ip}");
    $details = json_decode($json);
    return $details;
}

$details = ip_details("8.8.8.8");

echo $details->city;     // => Mountain View
echo $details->country;  // => US
echo $details->org;      // => AS15169 Google Inc.
echo $details->hostname; // => google-public-dns-a.google.com

I've used the IP 8.8.8.8 in these examples, but if you want details for the user's IP just pass in $_SERVER['REMOTE_ADDR'] instead. More details are available at http://ipinfo.io/developers

Ben Dowling
  • 17,187
  • 8
  • 87
  • 103
3

You can use the solution provided for this question.

But it returns a 2 digit country code.

Community
  • 1
  • 1
zak
  • 310
  • 3
  • 19
2

use the function ipToCountry($ip) from http://www.mmtutorialvault.com/php-ip-to-country-function/

saifur
  • 637
  • 4
  • 17
2

you can use web service API's which do this work like:

see example of service: http://ip-api.com and usage: http://whatmyip.info
1

See ipdata.co which gives you several data points from an IP address.

The API is pretty fast, with 10 global endpoints each being able to handle >800M calls daily.

Here's a curl example:

curl https://api.ipdata.co/78.8.53.5
{
    "ip": "78.8.53.5",
    "city": "G\u0142og\u00f3w",
    "region": "Lower Silesia",
    "region_code": "DS",
    "country_name": "Poland",
    "country_code": "PL",
    "continent_name": "Europe",
    "continent_code": "EU",
    "latitude": 51.6461,
    "longitude": 16.1678,
    "asn": "AS12741",
    "organisation": "Netia SA",
    "postal": "67-200",
    "currency": "PLN",
    "currency_symbol": "z\u0142",
    "calling_code": "48",
    "flag": "https://ipdata.co/flags/pl.png",
    "emoji_flag": "\ud83c\uddf5\ud83c\uddf1",
    "time_zone": "Europe/Warsaw",
    "is_eu": true,
    "suspicious_factors": {
        "is_tor": false
    }
}⏎  
Tanjim Ahmed Khan
  • 650
  • 1
  • 9
  • 21
Jonathan
  • 10,792
  • 5
  • 65
  • 85
1

You can try the free IP2Location LITE database

To create the table in MySQL

CREATE DATABASE ip2location;
USE ip2location;
CREATE TABLE `ip2location_db1`(
    `ip_from` INT(10) UNSIGNED,
    `ip_to` INT(10) UNSIGNED,
    `country_code` CHAR(2),
    `country_name` VARCHAR(64),
    INDEX `idx_ip_to` (`ip_to`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

To import the data

LOAD DATA LOCAL
    INFILE 'IP2LOCATION-LITE-DB1.CSV'
INTO TABLE
    `ip2location_db1`
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 0 LINES;

PHP code to query the MySQL

<?php
// Replace this MYSQL server variables with actual configuration
$mysql_server = "mysql_server.com";
$mysql_user_name = "UserName";
$mysql_user_pass = "Password";

// Retrieve visitor IP address from server variable REMOTE_ADDR
$ipaddress = $_SERVER["REMOTE_ADDR"];

// Convert IP address to IP number for querying database
$ipno = Dot2LongIP($ipaddress);

// Connect to the database server
$link = mysql_connect($mysql_server, $mysql_user_name, $mysql_user_pass) or die("Could not connect to MySQL database");

// Connect to the IP2Location database
mysql_select_db("ip2location") or die("Could not select database");

// SQL query string to match the recordset that the IP number fall between the valid range
$query = "SELECT * FROM ip2location_db1 WHERE $ipno <= ip_to LIMIT 1";

// Execute SQL query
$result = mysql_query($query) or die("IP2Location Query Failed");

// Retrieve the recordset (only one)
$row = mysql_fetch_object($result);

// Keep the country information into two different variables
$country_code = $row->country_code;
$country_name = $row->country_name;

echo "Country_code: " . $country_code . "<br/>";
echo "Country_name: " . $country_name . "<br />";

// Free recordset and close database connection
mysql_free_result($result);
mysql_close($link);

// Function to convert IP address (xxx.xxx.xxx.xxx) to IP number (0 to 256^4-1)
function Dot2LongIP ($IPaddr) {
 if ($IPaddr == "")
 {
   return 0;
 } else {
   $ips = explode(".", $IPaddr);
   return ($ips[3] + $ips[2] * 256 + $ips[1] * 256 * 256 + $ips[0] * 256 * 256 * 256);
 }
}
?>
Vlam
  • 1,622
  • 1
  • 8
  • 17
-1

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"
}
bre_dev
  • 562
  • 3
  • 21