GeoIP的使用 - PHP版

我们在做项目的时候,常常需要国家和地区的IP地址,下面通过介绍GeoIp的使用,让你轻松获取世界各地的国家和地区IP地址。

GeoIP介绍:

 

什么是GepIP ?

所谓GeoIP,就是通过来访者的IP, 定位他的经纬度,国家/地区,省市,甚至街道等位置信息。这里面的技术不算难题,关键在于有个精准 的数据库。有了准确的数据源就奇货可居赚点小钱,可是发扬合作精神,集体贡献众人享用是我们追求的。

方法1、

1)到官网:http://dev.maxmind.com/geoip/legacy/geolite/ 下载需要的版本

php Language/Platform Documentation Release History Package Repository Source Download Version Control Apache (mod_geoip) C C# Java Microsoft COM Perl Python
Documentation Releases   Source GitHub
Documentation Releases DebianUbuntu Source GitHub
Documentation Releases   Source GitHub
Documentation Releases Maven Source GitHub
Documentation (64-bit)     Source GitHub
Documentation Releases MetaCPAN   GitHub
Documentation Releases Composer Source GitHub
Documentation Releases   Source GitHub

2)http://dev.maxmind.com/geoip/legacy/geolite/ 下载 国家、城市ip库

  Download links Database Binary / gzip Binary / xz CSV / gzip CSV / zip CSV / xz
GeoLite Country Download Gzip only Zip only Download Zip only
GeoLite Country IPv6 Download Gzip only Download Gzip only Gzip only
GeoLite City Download Download Zip and xz only Download Download
GeoLite City IPv6 (Beta) Download Gzip only Download Gzip only Gzip only
GeoLite ASN Download Gzip only Zip only Download Zip only
GeoLite ASN IPv6 Download Gzip only Zip only Download Zip only

3)找到示例中 sample.php 和 sample_city.php 中 GeoIP.dat/GeoLiteCity.dat 路径为你自己的路径,进行测试

if( !defined( 'DOCUROOT' ) ){ define( 'DOCUROOT', str_replace("geoip","",dirname( __FILE__ ) ));}
include("geoipcity.inc");
include("geoipregionvars.php");

// uncomment for Shared Memory support
// geoip_load_shared_mem("/usr/local/share/GeoIP/GeoIPCity.dat");
// $gi = geoip_open("/usr/local/share/GeoIP/GeoIPCity.dat",GEOIP_SHARED_MEMORY);

$gi = geoip_open(DOCUROOT."geoipGeoLiteCity.dat",GEOIP_STANDARD);

$record = geoip_record_by_addr($gi,"68.32.235.26");
echo "you country code is:" .$record->country_code ;

geoip_close($gi);

方法2

把 GeoIP 安装成 php 扩展
yum install GeoIP GeoIP-data GeoIP-devel

下载 GeoIP 数据库
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gzip -d GeoLiteCity.dat.gz
mv GeoLiteCity.dat /var/lib/GeoIP/GeoIPCity.dat

下载 GeoIP 的 PECL 扩展
下载地址 http://pecl.php.net/package/geoip
wget -c http://pecl.php.net/get/geoip-1.0.7.tgz
tar -zxvf geoip-1.0.7.tgz

安 装 GeoIP 的 PECL 扩展
cd geoip-1.0.7
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-geoip
make
make install

在 php.ini 里加上
extension=geoip.so

接着重启一下 php 就行了
现在,你可以使用 php 手册里的 GeoIP 部份函数了

http://www.wapm.cn/phpdoc/zh/book.geoip.html geoip库方法介绍
 

转载:天道网络 » GeoIP库使用(php)

实例下载:geoip-api-php-master

数据下载:GeoIPv6.dat  |  GeoIP.dat

下载自我实例:ip_to_location(其实城市还是不太准确啊)

如果只是显示国家的话:

include("geoip.inc.php");
// 打开数据文件 
$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);
// 获取国家代码 
$country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']); 
echo "Your country code is: $country_code ";
// 获取国家名称 
$country_name = geoip_country_name_by_addr($gi, $_SERVER['REMOTE_ADDR']); 
echo "Your country name is: $country_name ";
// 关闭文件 
geoip_close($gi);

原文:GeoIP的使用 - PHP版

参考:Building an IP Geolocator with MaxMind GeoLite City

猜你喜欢

转载自justcoding.iteye.com/blog/2221884