`
sillycat
  • 浏览: 2556077 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Using Geo to locate the IP address

阅读更多
Using Geo to locate the IP address

>cpan
cpan>install Geo::IP::PurePerl

my examples are here: /root/.cpan/build/Geo-IP-PurePerl-1.25-yvkM42/t

# fetch latest GeoIP database, updated monthly
>wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
>gunzip GeoIP.dat.gz
>mkdir /usr/local/share/GeoIP
>mv GeoIP.dat /usr/local/share/GeoIP/GeoIP.dat
>chmod a+x /usr/local/share/GeoIP/GeoIP.dat

Geo::IP::PurePerl method

my $gi = Geo::IP::PurePerl->open("/usr/local/share/GeoIP/GeoIP.dat", GEOIP_STANDARD);
my $clientIp = $ENV{REMOTE_ADDR};
my $countryCode = $gi->country_code_by_name( $clientIp );
my $countryName = $gi->country_name_by_addr( $clientIp );
my $ispName = $gi->isp_by_addr($clientIp);
@data = $gi->get_city_record($clientIp);
my $record_str = join "," , @data;

my $q = CGI->new;
my $actionName = $q->param('actionName');

my $vars = {
    clientIp => $clientIp,
    countryCode => $countryCode,
    countryName => $countryName,
    records => $record_str,
    ispName => $ispName,
    remoteHost => $ENV{REMOTE_HOST},
    remotePort => $ENV{REMOTE_PORT},
    remoteUser => $ENV{REMOTE_USER},
    callingPage => $ENV{HTTP_REFERER},
    requestMethod => $ENV{REQUEST_METHOD},
    hostServerAddress => $ENV{HTTP_HOST},
    requestURI => $ENV{REQUEST_URI},
    userAgent => $ENV{HTTP_USER_AGENT},
    actionName => $actionName,
};

references:
http://www.maxmind.com/app/perl
http://cpan.uwinnipeg.ca/htdocs/Geo-IP-PurePerl/Geo/IP/PurePerl.html
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics