Wednesday, June 3, 2009

Track Geolocation of visitors: PHP and IP-to-Country Database

Geolocation is a geographical location of peer computer connected with some network i.e. local, or internet. That location can be anywhere in the world. When a computer gets connected to any network, a unique IP address is allocated and assigned to it, which uniquely identifies its identity on that network. IP address is a 32-bit number which is divided into 4 sections, each section is separated with a dot, and contain any number from 0-255. An IP address is very sensitive and complex to grab it all. It contains all the required information like network, location, host of the computer.

Webmasters and Bloggers are always interested to see whom visiting there site, where they came from, etc. There are some free tools on web to track your visitors. Google Analytics and Statcounter are two most popular and accurate tools for visitor tracking. They extract information like region, city, country, referral, ISP, etc from an IP address and provide us processed information in form of graphical components like chart, graphs and pie.

If you are interested to make such tool or you want to use it in your own way, here is the procedure to do it.


In this tutorial, I'll be assuming that you have little knowledge of PHP.
First of all, download the latest IP-to-Country CSV format database from here. CSV file consist of 5 fields begin IP number, ending IP number, 2-digit country code, 3-digit country code and country name. Eg.


"33996344","33996351","GB","GBR","UNITED KINGDOM""50331648","69956103","US","USA","UNITED STATES""69956104","69956111","BM","BMU","BERMUDA"

The range of record in which IP number will lies, that record will contain the other information like country name. Now, What is an IP number ? Is this same as the IP address. No, IP number is a number which is extracted from the IP address using a following formula, lets have a example if we have an IP address a.b.c.d, formula would be:
$ip_number = a x 16777216 + b x 65536 + c x 256 + d;
Well, we have discussed how it will work, now we will get to real stuff Lets! code it!

Download IP Locator 1.0, IP Locator is a small package, which allows programmer to use only just one function i.e. GetField(); for all the required operations.

Firstly include csv.php in your code:
require_once ('csv.php');
Below code shows three different ways of using GetField() function:
$two_digit_code = GetField('twodigitcode');$three_digit_code = GetField('threedigitcode');$country = GetField('country');
An example index.php is included to package, which shows the use of IP Locator in detail. Also noted that CSV database is split into 7 different files, for the faster processing and reduce the server load. Package contain the IP-to-Country database from http://ip-to-country.webhosting.info/ and the flags from www.ip2location.com.
Note: IPLocator v2.0 is available which now detects city, country, latitude, longitude, browser, OS etc and locate the user location in Google Maps. See the demo here. If you wish to buy this, contact me hireitgeek@gmail.com.

1 comment: