我们在做项目的时候,常常需要国家和地区的IP地址,下面通过介绍GeoIp的使用,让你轻松获取世界各地的国家和地区IP地址。
GeoIP介绍:
什么是GepIP ?
所谓GeoIP ,就是通过来访者的IP, 定位他的经纬度,国家/地区,省市,甚至街道等位置信息。这里面的技术不算难题,关键在于有个精准 的数据库。有了准确的数据源就奇货可居赚点小钱,可是发扬合作精神,集体贡献众人享用是我们追求的。
方法1、
1)到官网:http://dev.maxmind.com/geoip/legacy/geolite/ 下载需要的版本
Language/Platform
Documentation
Release History
Package Repository
Source Download
Version Control
Apache (mod_geoip)
Documentation
Releases
Source
GitHub
C
Documentation
Releases
Debian, Ubuntu
Source
GitHub
C#
Documentation
Releases
Source
GitHub
Java
Documentation
Releases
Maven
Source
GitHub
Microsoft COM
Documentation (64-bit)
Source
GitHub
Perl
Documentation
Releases
MetaCPAN
GitHub
php
Documentation
Releases
Composer
Source
GitHub
Python
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
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com