Author image Randy Kobes (contact brian d foy)

NAME

Apache::Geo::IP - Look up country by IP Address

SYNOPSIS

 # in httpd.conf
 # PerlModule Apache::HelloIP
 #<Location /ip>
 #   SetHandler perl-script
 #   PerlHandler Apache::HelloIP
 #   PerlSetVar GeoIPDBFile "/usr/local/share/geoip/GeoIP.dat"
 #   PerlSetVar GeoIPFlag Standard
 #</Location>
 
 # file Apache::HelloIP
  
 use Apache::Geo::IP;
 use strict;
 
 use Apache::Constants qw(OK);
 
 sub handler {
   my $r = Apache::Geo::IP->new(shift);
   $r->content_type('text/plain');
   my $country = uc($r->country_code_by_addr());
 
   $r->print($country);
  
   return OK;
 }
 1;

DESCRIPTION

This module constitutes a mod_perl (version 1) interface to the Geo::IP module, which looks up in a database a country of origin of an IP address. This database simply contains IP blocks as keys, and countries as values. This database should be more complete and accurate than reverse DNS lookups.

This module can be used to automatically select the geographically closest mirror, to analyze your web server logs to determine the countries of your visiters, for credit card fraud detection, and for software export controls.

To find a country for an IP address, this module a finds the Network that contains the IP address, then returns the country the Network is assigned to.

CONFIGURATION

This module subclasses Apache, and can be used as follows in an Apache module.

  # file Apache::HelloIP
  
  use Apache::Geo::IP;
  use strict;
  
  sub handler {
     my $r = Apache::Geo::IP->new(shift);
     # continue along
  }
 

The directives in httpd.conf are as follows:

  PerlModule Apache::HelloIP
  <Location /ip>
    PerlSetVar GeoIPDBFile "/usr/local/share/geoip/GeoIP.dat"
    PerlSetVar GeoIPFlag Standard
    # other directives
  </Location>
 

The PerlSetVar directives available are

PerlSetVar GeoIPDBFile "/path/to/GeoIP.dat"

This specifies the location of the GeoIP.dat file. If not given, it defaults to the location specified upon installing the module.

PerlSetVar GeoIPFlag Standard

This can be set to STANDARD, or for faster performance but at a cost of using more memory, MEMORY_CACHE. If not specified, STANDARD is used.

METHODS

The available methods are as follows.

$code = $r->country_code_by_addr( [$ipaddr] );

Returns the ISO 3166 country code for an IP address. If $ipaddr is not given, the value obtained by $r->connection->remote_ip is used.

$code = $r->country_code_by_name( [$ipname] );

Returns the ISO 3166 country code for a hostname. If $ipname is not given, the value obtained by $r->get_remote_host(REMOTE_HOST) is used.

$code = $r->country_code3_by_addr( [$ipaddr] );

Returns the 3 letter country code for an IP address. If $ipaddr is not given, the value obtained by $r->connection->remote_ip is used.

$code = $r->country_code3_by_name( [$ipname] );

Returns the 3 letter country code for a hostname. If $ipname is not given, the value obtained by $r->get_remote_host(REMOTE_HOST) is used.

$name = $r->country_name_by_addr( [$ipaddr] );

Returns the full country name for an IP address. If $ipaddr is not given, the value obtained by $r->connection->remote_ip is used.

$name = $r->country_name_by_name( [$ipname] );

Returns the full country name for a hostname. If $ipname is not given, the value obtained by $r->get_remote_host(REMOTE_HOST) is used.

VERSION

0.26

SEE ALSO

Geo::IP and Apache.

AUTHOR

The look-up code for associating a country with an IP address is based on the GeoIP library and the Geo::IP Perl module, and is Copyright (c) 2002, T.J. Mather, tjmather@tjmather.com, New York, NY, USA. See http://www.maxmind.com/ for details. The mod_perl interface is Copyright (c) 2002, Randy Kobes <randy@theoryx5.uwinnipeg.ca>.

All rights reserved. This package is free software; you can redistribute it and/or modify it under the same terms as Perl itself.