GeoIP的使用 - PHP版

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

GeoIP介绍:

 

什么是GepIP ?

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

 

方法1、

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

Language/PlatformDocumentationRelease HistoryPackage RepositorySource DownloadVersion Control
Apache (mod_geoip)DocumentationReleases SourceGitHub
CDocumentationReleases DebianUbuntu SourceGitHub
C#DocumentationReleases SourceGitHub
JavaDocumentationReleasesMavenSourceGitHub
Microsoft COM Documentation (64-bit)  SourceGitHub
PerlDocumentationReleasesMetaCPAN GitHub
phpDocumentationReleasesComposerSourceGitHub
PythonDocumentationReleases SourceGitHub

 

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

 Download links
DatabaseBinary / gzipBinary / xzCSV / gzipCSV / zipCSV / xz
GeoLite CountryDownloadGzip onlyZip onlyDownloadZip only
GeoLite Country IPv6DownloadGzip onlyDownloadGzip onlyGzip only
GeoLite CityDownloadDownloadZip and xz onlyDownloadDownload
GeoLite City IPv6 (Beta)DownloadGzip onlyDownloadGzip onlyGzip only
GeoLite ASNDownloadGzip onlyZip onlyDownloadZip only
GeoLite ASN IPv6DownloadGzip onlyZip onlyDownloadZip 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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值