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.
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.
Install geoip extension
sudo pecl install geoip
Add geoip extension to your php.ini configuration file.
sudo nano /etc/php5/apache2/php.ini
At the end of php.ini file add the following line.
Restart your apache server.
sudo /etc/init.d/apache2 restart
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.