Installation
Let’s install GeoIP PHP extention:
apt-get install php5-geoip
Next, we are should to download country database:
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz gunzip GeoIP.dat.gz mv GeoIP.dat /usr/share/GeoIP/
And city database:
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz gunzip GeoLiteCity.dat.gz mv GeoLiteCity.dat /usr/share/GeoIP/GeoIPCity.dat
Restarting php to apply changes:
service php5-fpm restart
PHP Example
<?php $details = geoip_record_by_name($_SERVER['REMOTE_ADDR']); echo $details['country_code']."|".$details['city']."|".$details['postal_code'];
See also
http://lite.ip2location.com/
http://php.net/manual/en/book.geoip.php