Project

General

Profile

IP Based Geographic Lookups

Module: mod_geoip

Requirements

Packages: GeoIP C API & Library (http://www.maxmind.com/download/geoip/api/c/)

Overview

mod_geoip is a module for fast ip/location lookups. It uses MaxMind GeoIP / GeoCity databases.
If the IP was found in the database, the module sets the appropriate environment variables to the request, thus making this information available to other modules/fcgi.

NOTE:
Currently only country/city databases are supported because MaxMind publishes a free version of its databases. (Thank you!)

http://www.maxmind.com/download/geoip/database/ - GeoIP.dat.gz for Countries, GeoLiteCity.dat.gz for Countries & Cities

Configuration Options

mod_geoip uses two configuration options.

  1. geoip.db-filename = <path to the geoip or geocity database>
  2. geoip.memory-cache = <enable|disable> : default disabled

if enabled, mod_geoip will load the database binary file to
memory for very fast lookups. the only penalty is memory usage.

NOTE: mod_geoip will determine the database type automatically so if you enter GeoCity database path it will load GeoCity Env.

Environment

Every database sets it's own ENV:

GeoIP (Country):

 GEOIP_COUNTRY_CODE
 GEOIP_COUNTRY_CODE3
 GEOIP_COUNTRY_NAME

GeoCity:

 GEOIP_COUNTRY_CODE
 GEOIP_COUNTRY_CODE3
 GEOIP_COUNTRY_NAME
 GEOIP_CITY_NAME
 GEOIP_CITY_POSTAL_CODE
 GEOIP_CITY_LATITUDE
 GEOIP_CITY_LONG_LATITUDE
 GEOIP_CITY_DMA_CODE
 GEOIP_CITY_AREA_CODE

Examples

mod_geoip + php

when using fastcgi (not only php) you can access mod_geoip env and do as you please. this example just prints all mod_geoip envs to the client, html.

Config-file

 geoip.db-filename = "/your-geoip-db-path/GeoLiteCity.dat" 
 geoip.memory-cache = "enable" 

index.php

 <?php
         $country_code = $_SERVER['GEOIP_COUNTRY_CODE'];
         $country_code3 = $_SERVER['GEOIP_COUNTRY_CODE3'];
         $country_name = $_SERVER['GEOIP_COUNTRY_NAME'];

         $city_region = $_SERVER['GEOIP_CITY_REGION'];
         $city_name = $_SERVER['GEOIP_CITY_NAME'];
         $city_postal_code = $_SERVER['GEOIP_CITY_POSTAL_CODE'];
         $city_latitude = $_SERVER['GEOIP_CITY_LATITUDE'];
         $city_long_latitude = $_SERVER['GEOIP_CITY_LONG_LATITUDE'];
         $city_dma_code = $_SERVER['GEOIP_CITY_DMA_CODE'];
         $city_area_code = $_SERVER['GEOIP_CITY_AREA_CODE'];

         echo "<html>\n<body>\n\t<br>\n";
         echo 'Country Code: ' . $country_code . '<br>';
         echo 'Country Code 3: ' . $country_code3 . '<br>';
         echo 'Country Name: ' . $country_name . '<br>';
         echo '<br>';
         echo 'City Region: ' . $city_region . '<br>';
         echo 'City Name: ' . $city_name . '<br>';
         echo 'City Postal Code: ' . $city_postal_code . '<br>';
         echo 'City Latitude: ' . $city_latitude . '<br>';
         echo 'City Long Latitude: ' . $city_long_latitude . '<br>';
         echo 'City DMA Code: ' . $city_dma_code . '<br>';
         echo 'City Area Code: ' . $city_area_code . '<br>';
         echo "</body>\n</html>";
 ?>

country based redirect

Config-file

 $HTTP["host"] =~ "www.domain.com" {
     url.rewrite = ( "" => "/redirect.php")
 }

redirect.php

 <?php
        $country_code = ( !empty( $_SERVER['GEOIP_COUNTRY_CODE'] ) ) ? $_SERVER['GEOIP_COUNTRY_CODE'] : 'US';
        header( 'Location: http://'.strtolower( $country_code ).'.domain.com/' );
        exit;
 ?>

Currently redirecting based on mod_geoip through the lighttpd config file is possible with some patches: http://trac.lighttpd.net/trac/ticket/1546. An alternative would be to use mod_magnet.

Downloads

mod_geoip_for_1.4.c View - mod_geoip for lighttpd 1.4 (13.9 KB) tobsn, 2009-01-09 19:52

mod_geoip_for_1.5.c View - mod_geoip for lighttpd 1.5 (13 KB) tobsn, 2009-01-09 19:52

mod_geoip-for-1.4.39.patch View (4.87 KB) gstrauss, 2016-02-29 22:26

mod_geoip_for_1.4.39.c View - mod_geoip for lighttpd 1.4.39 (14.1 KB) gstrauss, 2016-02-29 22:52

mod_geoip_for_1.4.40.patch View - mod_geoip for lighttpd 1.4.40 (18.5 KB) gstrauss, 2016-07-02 03:01