MaxMind
Support My Account FAQ My Order
Support My Account FAQ My Order

Apache Module
C
C# Class
Java Class
MS COM Object
Pascal
Perl Module
PHP Module
Python Class
Ruby Module
VB.Net
Support Center


GeoIP Perl API

Requirements
For Geo::IP, GeoIP C Library must be installed. Geo::IP::PurePerl is also available, which doesn't require the C Library and contains no XS code.

Download
Download Geo::IP Perl Module (faster, but requires C library)
Download Geo::IP::PurePerl Module (slower, but does not require C library)

Note: there have been reports of the Geo::IP 1.25 not working on FreeBSD, please try using Geo::IP 1.24 instead on FreeBSD.

Installing on Linux
This should work on most UNIX and GNU/Linux platforms.
perl Makefile.PL
make
make test
make install
More documentation is available from CPAN.

Installing on Windows
There is a PPM module available from Randy Kobes' PPM repository.

For ActivePerl 6xx builds, use:
C:\> ppm install http://theoryx5.uwinnipeg.ca/ppmpackages/Geo-IP.ppd
which can be accessed within the ppm shell by setting the repository to:
http://theoryx5.uwinnipeg.ca/cgi-bin/ppmserver?urn:/PPMServer
and then using:
ppm> install Geo-IP
For 8xx builds, use:
C:\> ppm install http://theoryx5.uwinnipeg.ca/ppms/Geo-IP.ppd
which can be accessed within the ppm shell by setting the repository to:
  http://theoryx5.uwinnipeg.ca/cgi-bin/ppmserver?urn:/PPMServer58
and then using:
ppm> install Geo-IP
In both cases, a post-install script should be run. The script will offer to fetch and install the GeoIP.dat database file; this is expected to go into the $ENV{programfiles}\GeoIP directory. If you don't run the post-install script, you can grab the database file from http://theoryx5.uwinnipeg.ca/ppms/scripts/ or http://theoryx5.uwinnipeg.ca/ppmpackages/scripts/.

Note there have been bug reports with using the GeoIP Perl API PPM. If you run into issues running the PPM on Windows, you may want to try the Geo::IP::PurePerl Module, which should run more reliably on Windows [Installation instructions].

Usage
Gets country name by hostname:
use Geo::IP;
my $gi = Geo::IP->new(GEOIP_STANDARD);
print $gi->country_name_by_name("amazon.com");
Gets US State by IP:
my $gi = Geo::IP->open("/usr/local/share/GeoIP/GeoIPRegion.dat", GEOIP_STANDARD);
my ($country, $region) = $gi->region_by_name("24.24.24.24");
Gets city by IP:
my $gi = Geo::IP->open("/usr/local/share/GeoIP/GeoIPCity.dat", GEOIP_STANDARD);
my $r = $gi->record_by_name("24.24.24.24");
print $record->country_code,
        $record->country_code3,
        $record->country_name,
        $record->region,
        $record->region_name,
        $record->city,
        $record->postal_code,
        $record->latitude,
        $record->longitude;
For additional examples, see t/ directory inside downloaded Perl API.


MaxMind, GeoIP and related marks are registered trademarks of MaxMind, Inc.
Copyright © 2008 MaxMind, Inc. All Rights Reserved. Terms of use.