Apache2::Geo::IP.3pm

Langue: en

Version: 2008-01-17 (mandriva - 01/05/08)

Section: 3 (Bibliothèques de fonctions)

NAME

Apache2::Geo::IP - Look up country by IP address

SYNOPSIS

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

DESCRIPTION

This module constitutes a mod_perl (version 2) 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.

If you are on Win32 and have installed this package using the ActivePerl ppm utility, the database GeoIP.dat is expected to be found under the C:\Program Files\GeoIP\ directory.

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 Apache2::RequestRec, and can be used as follows in an Apache module.
   # file Apache2::HelloIP
   
   use Apache2::Geo::IP;
   use strict;
  
   sub handler {
      my $r = Apache2::Geo::IP->new(shift);
      # continue along
   }
 
 

The directives in httpd.conf are as follows:

   <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. When using memory cache you can force a reload if the file is updated by using CHECK_CACHE. The INDEX_CACHE flag caches the most frequently accessed portion of the database. 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(Apache2::Const::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(Apache2::Const::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(Apache2::Const::REMOTE_HOST)" is used.

SEE ALSO

Geo::IP and Apache2::RequestRec.

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.