Update GeoIP information

The free version of MaxMind's GeoIP database and PHP application can be downloaded at their website. At www.condrau.com, the database is used for displaying the current IP address and to identify the origin of visitors traced by PHP I-Stats, a website statistics package written in PHP.

Go to http://www.maxmind.com/app/geolitecountry and download the latest database, the GeoLite Country Binary (GeoIP.dat.gz)

Go to http://www.maxmind.com/app/php and download the latest PHP script to read from the database (geoip.inc)

Note that there are different versions of the script, a pure PHP module, a module using PECL and a module using Apache the improve performance. For our purposes, the pure PHP module offers more than enough performance and is much easier to install and update.

Install update

Un-tar the database and store GeoIP.dat it in <I-STATS>/lib, then store geoip.inc in <I-STATS>/include. The browser process (www-data) needs to have read access to these files. (<I-STATS> is the install directory of PHP I-Stats. If I-Stats is not installed, the files can reside anywhere where they are accessible by the browser process)

Sample application

The following sample application uses GeoIP to display flag and origin of the visitor. In this example, PHP I-Stats is installed in /assets/stats/. The flags are stored in png-files and can be downloaded within PHP I-Stats at http://www.condrau.com/internet/downloadit/.

<?php
#-----------------------------------------------------------------------#
# Your current IP address                                               #
# =======================                                               #
#                                                                       #
# Copyright (c) 2007 by Bernard Condrau                                 #
# http://www.condrau.com/                                               #
#                                                                       #
#-----------------------------------------------------------------------#
# Last Update: 02 Mar 2007 14:30, Bernard Condrau                       #
#-----------------------------------------------------------------------#
#
define('INCLUDEPATH', '../../assets/stats/include/');
define('LIBPATH', '../../assets/stats/lib/');
define('WORLDPATH', '../../assets/stats/world/');
#
require_once(INCLUDEPATH . 'geoip.inc');
#
$handle = geoip_open(LIBPATH . "GeoIP.dat", GEOIP_STANDARD);
$country_code = geoip_country_code_by_addr($handle, $_SERVER['REMOTE_ADDR']);
$country_name = geoip_country_name_by_addr($handle, $_SERVER['REMOTE_ADDR']);
geoip_close($handle);
#
$worldstr = WORLDPATH.strtolower($country_code).'.png';
#
echo "<p>Your <b>IP</b> address is: <b>".$_SERVER['REMOTE_ADDR']."</b>, which is registered in ";
echo "<img src=$worldstr><b> ".$country_name."</b></p>";
?>