How to do GeoIP the correct way with marginal overhead.

MaxMind’s mod_geoip directly inserted into your webserver would be the easiest for external/programmable GeoIP support.

It’d make more sense to check the browser’s language(s) of choice, than force a user of a region into a language they don’t want. Let Apache handle the language, itself, then just display the runtime code (if that’s what you want to do) independently with mod_geoip.

First, setup your supported languages, and priority in Apache:

AddLanguage en .en
AddLanguage fr .fr
AddLanguage es .es
AddLanguage de .de

Then, set your defaults you want to support in order of priority:

LanguagePriority en fr es de

Create multiple pages with the different languages embedded. “index.html” would be removed, and you would have “index.html.en”, “index.html.fr”, “index.html.es”, and “index.html.de” (This also works for .php, .shtml, etc.)

Now, GeoIP’s database for the country name database part, which almost mimmics the above, but uses a different format, ISO3166.

Here’s some tiny little throwaway GeoIP code. It displays those tiny flags in my sig. It has very little overhead with mod_geoip, marginal with the runtime PHP library. This is an old version without eTags (caching) support. It’d be smarter to cache these into RAM than to continually read them from the disk, if you have the choice. You may use this code, if you want.

<?php
// Simple little example utlity to display a small flag based upon GeoIP data.
// by Shawn Holwegner <shawn/at/holwegner/dot/com>
// You may reuse this code, if you want.  I stake no claims to it.
//
// This example supports both mod_geoip, and the external PHP API.
// If mod_geoip is installed:
  $country = "";
  // Do we have apache_note()?
  if (function_exists('apache_note'))
    $country = strtolower(apache_note("GEOIP_COUNTRY_CODE"));
  if (empty($country)) {
    // external MaxMind PHP support
    @require_once("./geoip.inc");
    $gi = geoip_open("./GeoIP.dat", GEOIP_STANDARD);
    $country = strtolower(geoip_country_code_by_addr($gi, $_SERVER["REMOTE_ADDR"
]));
    geoip_close($gi);
    if ($country) {
      header ("Pragma: no-cache");
      header("Expires: Mon, 26 Jul 1999 04:20:00 GMT");
      header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
      header('Cache-Control: no-store, no-cache, must-revalidate');
      header('Cache-Control: pre-check=0, post-check=0, max-age=0');
      header("Content-type: image/gif");
      if (file_exists('./'.$country.'.gif')) {
         readfile('./'.$country.'.gif');
      } else {
         // Generic 'empty' country banner.
         readfile("./00.gif");
      }
    }
  }
?>

I hope this information has been helpful as to easily, and properly implement GeoIP support. Stop forcing me to read Spanish when I’m in Latin America; I get enough of that when I’m in the states!