Organizational Research By

Surprising Reserch Topic

get country of ip address with php

get country of ip address with php  using -'php,geolocation,ip-address,geoip,ip-geolocation'

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?

asked Sep 9, 2015 by rajesh
0 votes

Related Hot Questions

9 Answers

0 votes

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:


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.

answered Sep 9, 2015 by rajesh
0 votes

There are various web APIs that will do this for you. Here's an example using

$details = json_decode(file_get_contents("{$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.

answered Sep 9, 2015 by rajesh
0 votes

You can download ip-tables from MaxMind:

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
    SELECT country
    FROM ip_table
    WHERE $iplong BETWEEN ip_long_from AND ip_long_to
    LIMIT 1
answered Sep 9, 2015 by rajesh
0 votes

All solutions above depend on Free API's which are not that reliable in most cases and also have limitations. For a reliable and an unlimited solution and faster performance you can create your own database.

1) Import the SQL table in the link below to your database.

2) Get user's IP and convert it to an integer.

       $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
       $ip = $_SERVER['HTTP_CLIENT_IP'];
       $iparray = explode('.', $ip);
       $ipdecimal = ($iparray[0] * 16777216) + ($iparray[1] * 65536) + ($iparray[2] * 256) + ($iparray[3]);

3) Query for the user's IP integer in the SQL ranges.

$sql="SELECT code,name FROM ip2country WHERE ".$ipdecimal." BETWEEN min AND max";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$usercountrycode = $row[0];
$usercountryname = $row[1];

That's it!

echo "You live in".$usercountryname." - ".$usercountrycode;
/*will return something like: You live in Autsralia - AU*/

Enjoy your own free, reliable and unlimited IP lookup tool.

answered Sep 9, 2015 by rajesh
0 votes

Here's an example using

$details = json_decode(file_get_contents("{$ip}"));
echo $details;
answered Sep 9, 2015 by rajesh
0 votes

Here is another free API for geolocation requests:

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

answered Sep 9, 2015 by rajesh
0 votes

I found this blog post very helpful. It saved my day.

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

answered Sep 9, 2015 by rajesh
0 votes

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: equal to 45815275)

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


API Service:

answered Sep 9, 2015 by rajesh
0 votes

I see there are a lot of answers, but no one of them mentions

You're allowed up to 10,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.

Moreover, 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.

answered Sep 9, 2015 by rajesh