Using Geo to locate the IP address

本文介绍如何使用Perl模块Geo::IP::PurePerl进行IP地址的地理定位,包括安装配置及基本使用方法,如获取客户端IP地址对应的国家代码、国家名称及ISP信息等。
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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值