summaryrefslogtreecommitdiff
path: root/doc/geoip.txt
blob: 99b0ed096e5cba36525000f0ca9a777b4f5e98e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
{{{
#!rst
==============================
ip based geographic lookups...
==============================

-----------------
Module: mod_geoip
-----------------



.. contents:: Table of Contents

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
environments variables to the request, thus making other modules/fcgi be
informed.

.. note::

  Currently only country/city databases are supported because they have a free
  version that i can test.

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 databse 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/GeoCityLite.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 "</html>\n</body>";
 ?>

country based redirect
----------------------

Config-file ::

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

redirect.php ::

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

.. note::

 Currently it is not possible to redirect based on mod_geoip directly in
lighttpd config file. But i believe with the relase of lighttpd mod_magnet
it would be. (mod_magnet will be available in lighttpd 1.4.12+)

Downloads
=========
mod_geoip.c (http://trac.lighttpd.net/trac/attachment/wiki/Docs/ModGeoip/mod_geoip.c)
}}}