http://php.net/manual/zh/ref.geoip.php
sudo apt-get install libgeoip-dev
pecl install geoip-1.1.0 注意:Beta版要指定版本号.如果是apt安装的PHP,直接安装php5-geoip这个包即可.
php.ini中加入:
extension=geoip.so
geoip.custom_directory="/usr/share/GeoIP"
下载GeoLiteCity数据库(解压后18MB):
http://dev.maxmind.com/geoip/legacy/install/city/
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gunzip GeoLiteCity.dat.gz
sudo mkdir -v /usr/share/GeoIP
sudo mv -v GeoLiteCity.dat /usr/share/GeoIP/GeoIPCity.dat
测试:
php -a
<?php
print_r(geoip_record_by_name('106.37.165.80')); 回车后按Ctrl+D运行
Array
(
[continent_code] => AS
[country_code] => CN
[country_code3] => CHN
[country_name] => China //国家
[region] => 22
[city] => Beijing //城市
[postal_code] =>
[latitude] => 39.928901672363 //纬度
[longitude] => 116.38829803467 //经度
[dma_code] => 0
[area_code] => 0
)
在命令行用geoiplookup查看IP信息:
traceroute www.oschina.net 可见IP地址 61.145.122.155
sudo apt-get install geoip-bin geoip-database
geoiplookup 61.145.122.155 -f /usr/share/GeoIP/GeoIP.dat
GeoIP Country Edition: CN, China
geoip-database提供的GeoIP.dat只能精确到国家.
geoiplookup 61.145.122.155 -f /usr/share/GeoIP/GeoIPCity.dat
GeoIP City Edition, Rev 1: CN, 30, Guangdong, Guangzhou, N/A, 23.116699, 113.250000, 0, 0
从maxmind官网下的数据库GeoLiteCity则信息更详细.
geoiplookup 61.145.122.155 则同时显示上述两个数据库的信息.