Track your visitors by GeoIP - Part1

Date: 2010-11-15

In this article I am going to describe how get the data from your visitors based on geographic location. I'll use PECL extension for PHP called geoip. Step 1.
First we need to install PEAR package (if you've not installed it yet). We also need to install needed dependencies and libraries like re2c, libgeoip1 and libgeoip-dev.

Code:

sudo apt-get install php5-dev php-pear re2c libgeoip1 libgeoip-dev

Now you are able to run "pecl" command at the prompt.

Step 2.
Install geoip extension

Code:

sudo pecl install geoip

Step 3.
Add geoip extension to your php.ini configuration file.

Code:

sudo nano /etc/php5/apache2/php.ini

Step 4.
At the end of php.ini file add the following line.

Code:

extension=geoip.so

Step 5.
Restart your apache server.

Code:

sudo /etc/init.d/apache2 restart

Summary:
Now you have a fresh installation of PEAR-package and geoip extension on your system. To test that you have successfully installed geoip extension; create a php file that prints phpinfo(); function and verify geoip exists in the info list. In the next part we will create a PHP script to display country based on visitors IP-number.