Skip to content
Nosce Te Ipsum
  • Home
  • About me
  • My COETAIL Blogs
    • Course 1
    • Course 2
    • Course 3
    • Course 4
  • Testimonials
    • Professional
    • Personal
Site Search

Using GeoIP database to identify site visitor’s country/city

  • January 3, 2017January 3, 2017
  • by Muhammad
Share on Facebook
Facebook
Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin
Pin on Pinterest
Pinterest
Share on Reddit
Reddit
Email this to someone
email
Share on StumbleUpon
StumbleUpon
Digg this
Digg

GeoIP by Maxmind is offered in two versions – a paid and a free one. Currently GoeIP API ver. 2 is used to use either of those databases.
Maxmind’s URL for this is
http://dev.maxmind.com/geoip/geoip2/geolite2/

For this install we are using the free version following a guide found on these two pages:

http://ctrtard.com/code/how-to-install-the-maxmind-geoip2-database-for-php/
https://github.com/maxmind/GeoIP2-php

Before starting, we need to install curl and php-development tools

apt-get install curl php5-dev

I installed it on a Debian system and carried out all the install in /var/www folder.
I use the Country database. You can use the City one if you want/need
Here are the commands

cd /var/www/
sudo mkdir geoip2lite
cd geoip2lite/
sudo wget http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz
sudo gunzip GeoLite2-Country.mmdb.gz
sudo mkdir /usr/local/share/GeoIP
sudo mv GeoLite2-Country.mmdb /usr/local/share/GeoIP/

This downloads the database from maxmind site, unpacks it and copies it to the folder from where it can be used

Next is to get the PHP API to use this database

sudo curl -sS https://getcomposer.org/installer | php
sudo php composer.phar require geoip2/geoip2:~2.0

Now cat or vim the composer.json file to make sure it has following lines. If it doesn’t, then add them manually

{
"require": {
"geoip2/geoip2": "~2.0"
}
}

and then

sudo php composer.phar install

By this time you should have following files and folders in your /var/www/geoip2lite folder

composer.json composer.lock composer.phar libmaxminddb vendor

Installing C libaray for faster lookups

cd libmaxminddb
sudo apt-get install git

sudo git clone --recursive https://github.com/maxmind/libmaxminddb

sudo apt-get install autoconf
sudo apt-get install dh-autoreconf

sudo ./bootstrap
sudo ./configure
sudo make check
sudo make install
sudo ldconfig

Installing PHP C Extension

cd ..
cd vendor
cd maxmind-db/reader/
cd ext
sudo phpize
sudo ./configure
sudo make
sudo make test
sudo make install

Now you got to add this extension in php.ini and to know where is the file located create a small phpinfo.php file with code below

<?php
// Show all information, defaults to INFO_ALL
phpinfo();
?>

load this file in browser and look for

Loaded Configuration File

/etc/php5/fpm/php.ini

Edit this file and add the following line at the end of file.

extension=maxminddb.so

And run this command

sudo ldconfig /usr/local/lib

Once done, reload apache by

sudo service apache2 restart

And check the phpinfo.php file again. You should search and find the following block

geoip

geoip support

enabled

geoip extension version

1.1.0

geoip library version

1006002

Directive

Local Value

Master Value

geoip.custom_directory

no value

no value

And it’s done

Now whichever folder (site) requires this GeoIP lookup, copy all composer files (composer.*) and vendor folder to that site’s folder.

You can test with following code in a test.php file
Remember to change the name of database file if you used the city database

<?php
require_once ‘vendor/autoload.php’;
use GeoIp2\Database\Reader;
$reader = new Reader(‘/usr/local/share/GeoIP/GeoLite2-Country.mmdb’);
$record = $reader->country($_SERVER[‘REMOTE_ADDR’]);
print($record->country->isoCode . “\n”);
print($record->country->name . “\n”);
?>

Share on Facebook
Facebook
Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin
Pin on Pinterest
Pinterest
Share on Reddit
Reddit
Email this to someone
email
Share on StumbleUpon
StumbleUpon
Digg this
Digg
Ellen Carberry
Mathew Cramer (Greenford High School – U.K.)
Muhammad

Related articles

Raspberry PI as temperature and…
Dynamic DNS client setup for…
Install Nginx, MySQL and PHP…
Autostart a terminal app on…
Attaching an external storage with…
Setting up L2TP/IPSEC server using…
(c) Raspberry Pi
Starting with Raspberry Pi
Designing Technology Infrastructure for Transformational…
The sad state of human…
Installing RD9700 chipset based USB…

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Me!

Follow Me On TwitterFollow Me On LinkedInFollow Me On PinterestFollow Me On About.meFollow Me On Wordpress
May 2025
M T W T F S S
« Oct    
 1234
567891011
12131415161718
19202122232425
262728293031  

2009 2010 2011 Amelie apache beijing birthday clearos crèpes debian december 2009 dubai email february firewall guide holidays ill install internet kuala lumpur linux list mac maheen mobile october 2009 outlook Pakistan raspberry raspberry pi router search Sharepoint smtp snow spring ubuntu unix VLAN VPN windows 7 winter xian Zimbra

WP Cumulus Flash tag cloud by Roy Tanck requires Flash Player 9 or better.

Categories

Licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Theme by Colorlib Powered by WordPress
  • Home
  • About me
  • My COETAIL Blogs
  • Testimonials