Home‎ > ‎PHP‎ > ‎

Installing PHP GeoIP on Ubuntu - only necessary steps

Advertisement:

Canagon.com - Website for $499, beautiful premade designs, high-performance hosting on all continents, dedicated support team. Learn more >

To make GeoIP work on PHP ( all the functions in http://php.net/manual/en/book.geoip.php ), php5-geoip and GeoCityLite.dat must be installed. 

Some websites have wrong instructions advising you to download GeoCityLite.dat to /usr/share/GeoIP - wrong, it should be /usr/local/share/GeoIP 

# apt-get install php5-geoip
# mkdir /usr/local/share/GeoIP
# cd /usr/share/local/GeoIP
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
# gunzip GeoLiteCity.dat.gz
# service apache2 restart

It should work, from now on you should be able to use PHP Geo IP functions, be ware that if you test it from localhost, your IP will be 192.168..., upload your script to server first. 

<?php 

$ip_address = $_SERVER['REMOTE_ADDR'];
echo"$ip_address, ";
$ip_country = geoip_country_code_by_name ($ip_address);
echo"$ip_country";

?>

This should output your IP address and two-letter country code X.X.X.X, CC. See PHP manual for more functions http://php.net/manual/en/ref.geoip.php like city or ISP.