Installing MaxMind’s GeoIP on Debian / Ubuntu is too easy.

sudo apt-get install php5-geoip sudo /etc/init.d/apache2 restart

This installs the database to /usr/share/GeoIP/GeoIP.dat. You can now test it via  a simple PHP script like

Or use a bit more complex script like the following which first looks up the current IP address of the user and then checks in which country he is.

php $ip = ''; if (isset($_SERVER["REMOTE_ADDR"])) { $ip = $_SERVER["REMOTE_ADDR"]; } else if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) { $ip = $_SERVER["HTTP_X_FORWARDED_FOR"]; } else if (isset($_SERVER["HTTP_CLIENT_IP"])) { $ip = $_SERVER["HTTP_CLIENT_IP"]; } if (!empty($ip)) { $country = geoip_country_code_by_name($ip); if ($country) { echo 'This host is located in: ' . $country; } else { echo 'Sorry, this IP address is not in the database'; } } Happy programming.