Get Country of IP Address with PHP

PhpGeolocationIp AddressGeoipIp Geolocation

Php Problem Overview


Ideally I'm trying to put together a PHP script that I can query from any web browser and it returns the country of the IP address that accessed the PHP script.

Is this possible or is there a better solution?

Php Solutions


Solution 1 - Php

There are free, easy APIs you can use, like those:

Which one looks the most trustworthy is up to you :)

Otherwise, there are scripts which are based on local databases on your server. The database data needs to be updated regularly, though. Check out this one:

HTH!

Edit: And of course, depending on your project you might want to look at HTML5 Location features. You can't use them yet on the Internet Explorer (IE9 will support it, long way to go yet), but in case your audience is mainly on mobile devices or using Safari/Firefox it's definitely worth to look at it!

Once you have the coordinates, you can reverse geocode them to a country code. Again there are APIs like this one:

Update, April 2013
Today I would recommend using Geocoder, a PHP library which makes it very easy to geocode ip addresses as well as postal address data.

***Update, September 2016
Since Google's privacy politics has changed, you can't use HTML5 Geolocation API if your server doesn't have HTPPS certificate or user doesn't allow you check his location. So now you can use user's IP and check in in PHP or get HTTPS certificate.

Solution 2 - Php

There are various web APIs that will do this for you. Here's an example using my service, http://ipinfo.io:

$ip = $_SERVER['REMOTE_ADDR'];
$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}"));
echo $details->country; // -> "US"

Web APIs are a nice quick and easy solution, but if you need to do a lot of lookups then having an IP -> country database on your own machine is a better solution. MaxMind offer a free database that you can use with various PHP libraries, including GeoIP.

Solution 3 - Php

You can download ip-tables from MaxMind:

http://www.maxmind.com/app/geolite

Import the CSV to your database (make sure to create an index for ip_long_from + ip_long_to). Then you can simply do:

$iplong = ip2long($_SERVER['REMOTE_ADDR']);

// should use mysqli with prepared statements etc, but just an example
mysql_query("
	SELECT country
	FROM ip_table
	WHERE $iplong BETWEEN ip_long_from AND ip_long_to
	LIMIT 1
");

Solution 4 - Php

Here's an example using http://www.geoplugin.net/json.gp

$ip = $_SERVER['REMOTE_ADDR'];
$details = json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip={$ip}"));
echo $details;

Solution 5 - Php

You can get country from IP address with this location API

Code

    echo file_get_contents('https://ipapi.co/8.8.8.8/country/');

Response

   US 

Here's a fiddle. Response is text when you query a specific field e.g. country here. No decoding needed, just plug it into your code.

P.S. If you want all the fields e.g. https://ipapi.co/8.8.8.8/json/, the response is JSON.

Solution 6 - Php

Look at the GeoIP functions under "Other Basic Extensions." http://php.net/manual/en/book.geoip.php

Solution 7 - Php

I see there are a lot of answers, but no one of them mentions https://freegeoip.net/

You're allowed up to 15,000 queries per hour. I think that's enough in most of the cases. Otherwise, you should think about using local databases from MaxMind GeoIP.

Freegeoip is open-source and you can find sources on github, deploy it locally and get rid of the query limits.

Also, service provides latitude/longitude/timezone and other useful stuff.

Solution 8 - Php

$country_code = trim(file_get_contents("http://ipinfo.io/{$ip_address}/country"))

will get you the country code.

Solution 9 - Php

Here is another free API for geolocation requests: http://geoip.nekudo.com

Responses are in JSON format. Additionally the API sourcecode is available on github in case you need to setup your own API.

Solution 10 - Php

Install and use PHP's GeoIP extension if you can. On debian lenny:

sudo wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
sudo gunzip GeoLiteCity.dat.gz
sudo mkdir -v /usr/share/GeoIP
sudo mv -v GeoLiteCity.dat /usr/share/GeoIP/GeoIPCity.dat

sudo apt-get install php5-geoip
# or sudo apt-get install php-geoip for PHP7

and then try it in PHP:

$ip = $_SERVER['REMOTE_ADDR'];
$country = geoip_country_name_by_name($ip);
echo 'The current user is located in: ' . $country;

returns:

The current user is located in: Cameroon

Solution 11 - Php

I found this blog post very helpful. It saved my day. http://www.techsirius.com/2014/05/simple-geo-tracking-system.html

Basically you need to install the IP database table and then do mysql query for the IP for location.

<?php
include 'config.php';
mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) OR die(mysql_error());
mysql_select_db(DB_NAME) OR die(mysql_error());
$ip =  $_SERVER['REMOTE_ADDR'];
$long = sprintf('%u', ip2long($ip));
$sql = "SELECT * FROM `geo_ips` WHERE '$long' BETWEEN `ip_start` AND `ip_end`";
$result = mysql_query($sql) OR die(mysql_error());
$ip_detail = mysql_fetch_assoc($result);
if($ip_detail){
    echo $ip_detail['country']."', '".$ip_detail['state']."', '".$ip_detail['city'];
}
else{
    //Something wrong with IP
}

Solution 12 - Php

If you need to have a good and updated database, for having more performance and not requesting external services from your website, here there is a good place to download updated and accurate databases.

I'm recommending this because in my experience it was working excellent even including city and country location for my IP which most of other databases/apis failed to include/report my location except this service which directed me to this database.
(My ip location was from "Rasht" City in "Iran" when I was testing and the ip was: 2.187.21.235 equal to 45815275)

Therefore I recommend to use these services if you're unsure about which service is more accurate:

Database: http://lite.ip2location.com/databases

API Service: http://ipinfodb.com/ip_location_api.php

Solution 13 - Php

You can try the services of https://www.geoip-db.com They provide a JSON or JSONP-callback solution.

<?php

    $json = file_get_contents("https://www.geoip-db.com/json");
    $data = json_decode($json);

    print $data->country_code . "<br>";
    print $data->country_name . "<br>";
    print $data->state . "<br>";
    print $data->city . "<br>";
    print $data->postal . "<br>";
    print $data->latitude . "<br>";
    print $data->longitude . "<br>";
    print $data->IPv4 . "<br>";

?>

Solution 14 - Php

You can use <https://ip-api.io/> to get country name, city name, latitude and longitude. It supports IPv6.

As a bonus it will tell if ip address is a tor node, public proxy or spammer.

Php Code:

$result = json_decode(file_get_contents('http://ip-api.io/json/64.30.228.118'));
var_dump($result);

Output:

{
"ip": "64.30.228.118",
"country_code": "US",
"country_name": "United States",
"region_code": "FL",
"region_name": "Florida",
"city": "Fort Lauderdale",
"zip_code": "33309",
"time_zone": "America/New_York",
"latitude": 26.1882,
"longitude": -80.1711,
"metro_code": 528,
"suspicious_factors": {
"is_proxy": false,
"is_tor_node": false,
"is_spam": false,
"is_suspicious": false
}

Solution 15 - Php

I run the service at IPLocate.io, which you can hook into for free with one easy call:

<?php
$res = file_get_contents('https://www.iplocate.io/api/lookup/8.8.8.8');
$res = json_decode($res);

echo $res->country; // United States
echo $res->continent; // North America
echo $res->latitude; // 37.751
echo $res->longitude; // -97.822

var_dump($res);

The $res object will contain your geolocation fields like country, city, etc.

Check out the docs for more information.

Solution 16 - Php

Ipdata.co provides a scalable API to do this. With 10 global endpoints each able to handle >800M requests daily!

It also gives you the organisation, currency, timezone, calling code, flag and Tor Exit Node status data from any IPv4 or IPv6 address.

In php

php > $ip = '8.8.8.8';
php > $details = json_decode(file_get_contents("https://api.ipdata.co/{$ip}"));
php > echo $details->region;
California
php > echo $details->city;
Mountain View
php > echo $details->country_name;
United States
php > echo $details->location;
37.405991,-122.078514

Disclaimer

I built this service.

Solution 17 - Php

Use the widget www.feedsalive.com, to get the country informations & last page information as well.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTristanView Question on Stackoverflow
Solution 1 - PhpsprainView Answer on Stackoverflow
Solution 2 - PhpBen DowlingView Answer on Stackoverflow
Solution 3 - PhpquanoView Answer on Stackoverflow
Solution 4 - PhpchannasmcsView Answer on Stackoverflow
Solution 5 - PhpFrancisView Answer on Stackoverflow
Solution 6 - PhpJohn FranklinView Answer on Stackoverflow
Solution 7 - Phpivan.mylyanykView Answer on Stackoverflow
Solution 8 - PhpMohammad AniniView Answer on Stackoverflow
Solution 9 - PhpnekudoView Answer on Stackoverflow
Solution 10 - PhpCedric IpkissView Answer on Stackoverflow
Solution 11 - PhpWebbyView Answer on Stackoverflow
Solution 12 - PhpMojtaba RezaeianView Answer on Stackoverflow
Solution 13 - PhpKurt Van den BrandenView Answer on Stackoverflow
Solution 14 - PhpVindhyachal KumarView Answer on Stackoverflow
Solution 15 - PhpttarikView Answer on Stackoverflow
Solution 16 - PhpJonathanView Answer on Stackoverflow
Solution 17 - PhpShaView Answer on Stackoverflow