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
>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
本文介绍如何使用Perl模块Geo::IP::PurePerl进行IP地址的地理定位,包括安装配置及基本使用方法,如获取客户端IP地址对应的国家代码、国家名称及ISP信息等。

被折叠的 条评论
为什么被折叠?



