Track your visitors by GeoIP – Part1

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. 

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

sudo pecl install geoip

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

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

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

extension=geoip.so

Step 5
Restart your apache server.

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.