Monday 27 May 2013
Facebook StumbleUpon Twitter Google+ Pin It

IP address to geographic details


Today I received a small project to detect the country of web visitors. I’ve never done something like that before, so as always, I started googling around. And it turned out that it’s not that complex – there is a company called MaxMind that offers APIs to look up geographic details from an IP address. The APIs are written in a variety of languages like C, Perl, Java, Python, Ruby, C#, VB.NET, Pascal, Javascript, and of course, PHP.
For this project, I decided to go with PHP version which has a NET_GeoIP PEAR package. Just download it, along with the free data files (they have paid ones also) – Geo Lite Country if you want the country details only, or Geo Lite City if you want extra stuffs like city, region, latitude and longtitude etc. In this post, I’ll go with City version.
Now, with everything downloaded and extracted into a folder, start with an empty index.php file and fill it with some code:
<?php
require_once "path/to/file/GeoIP.php";
/ define a new instance of Net_GeoIP class
/$geoip = Net_GeoIP::getInstance("path/to/file/GeoLiteCity.dat");
try {
_dump($geoip->lookupLocation($_SERVER['REMOTE_ADDR'])); } catc
va rh (Exception $e) { // Handle exception }
?>
The function lookupLocation() from class NET_GeoIP accepts a string parameter in IP format – something like “210.245.17.13″ and returns a Location object which contains several useful geographic information. If you run this script on your localhost however, nothing will be printed. That is because in such a case, $_SERVER['REMOTE_ADDR'] is a local IP – most often 127.0.0.1 – which cannot tell us anything. Have you heard this quote “There is nothing like 127.0.0.1″?
So for testing purpose, let’s modify the above code a bit, so that it can accept an optional query parameter as an IP.
<?php
require_once "path/to/file/GeoIP.php";
/ define a new instance of Net_GeoIP class
/$geoip = Net_GeoIP::getInstance("path/to/file/GeoLiteCity.dat");
ip = isset($_GET['test_ip']) ? $_GET['test_ip'] : $_SERVER['REMOTE_ADDR'];
$ try { var_dump($geoip->lookupLocation($ip)); } catch (Exception $e) {
// Handle exception }
?>
Now if you’ve set it everything correctly in place, browse to index.php?test_ip=210.124.12.3 will show something like this:
<code>object(Net_GeoIP_Location)#2 (10) {
["countryCode"]=&gt;
string(2) "KR"
["countryName"]=&gt;
string(18) "Korea, Republic of"
["region"]=&gt;
string(2) "11"
["city"]=&gt;
string(5) "Seoul"
["postalCode"]=&gt;
NULL
["latitude"]=&gt;
float(37.5664)
["longitude"]=&gt;
float(126.9997)
["areaCode"]=&gt;
NULL
["dmaCode"]=&gt;
NULL
["countryCode3"]=&gt;
string(3) "KOR"
}</code>
Similarly, index.php?test_ip=128.54.13.8 shows
<code>object(Net_GeoIP_Location)#2 (10) {
["countryCode"]=&gt;
string(2) "US"
["countryName"]=&gt;
string(13) "United States"
["region"]=&gt;
string(2) "CA"
["city"]=&gt;
string(8) "La Jolla"
["postalCode"]=&gt;
string(5) "92093"
["latitude"]=&gt;
float(32.8807)
["longitude"]=&gt;
float(-117.2359)
["areaCode"]=&gt;
int(858)
["dmaCode"]=&gt;
float(825)
["countryCode3"]=&gt;
string(3) "USA"
}</code>
Extremely useful, isn’t it? With these information on hand, there’s no limit of what we can do to improve user interaction and much more.

No comments: