加入收藏 | 设为首页 | 会员中心 | 我要投稿 武陵站长网 (https://www.50888.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php应用GeoIP库实例教程

发布时间:2022-10-17 04:32:16 所属栏目:PHP教程 来源:互联网
导读: php应用GeoIP库实例教程
注意php实例教程,maxmind是geoip数据库的提供方,同时也提供了很多语言的sample和api说明文档。比如php,和php的geoip库是有很大区别的,包含使用方式,接口函数。

php应用GeoIP库实例教程

注意php实例教程,maxmind是geoip数据库的提供方,同时也提供了很多语言的sample和api说明文档。比如php,和php的geoip库是有很大区别的,包含使用方式,接口函数。php官方的geoip需要配置php环境,加载geoip.dll库,并在php.ini中指定GEOIP库地址。maxmind提供一系列 .inc 和 .php 对环境无依赖,只要支持php,直接require后即可使用。(php GeoIP库入门实例)

一、GEOIP数据库细分到国家:GeoLite Country细分到城市:GeoLite City

二、php官方geoip.dll库下载dll 修改php.ini,启用geoip库:

extension=php_geoip.dll

复制代码

追加geoip段,指定数据库位置:

[geoip]geoip.custom_directory = "D:\php5.3\geo\"

复制代码

测试代码注意geoip.dll中使用的 GeoIPCity.dat 即 GeoLiteCity.dat,使用时注意看提示。

echo geoip_country_name_by_name( "8.8.8.8" ) . "\n";print_r( geoip_record_by_name( "8.8.8.8" ) );echo geoip_country_name_by_name( "61.139.2.69" ). "\n";print_r( geoip_record_by_name( "61.139.2.69" ) );

复制代码

三、MaxMind官方php文件函数库文档和示例:修改maxmind示例中 sample.php 和 sample_city.php 中 GeoIP.dat/GeoLiteCity.dat 路径为你自己的路径同一目录用 “./GeoIP.dat” 或 “./GeoLiteCity.dat” 即可。

1,详细到国家

include("geoip.inc");$gi = geoip_open( "./GeoIP.dat", GEOIP_STANDARD );echo geoip_country_code_by_addr($gi, "8.8.8.8") . "\t" . geoip_country_name_by_addr($gi, "8.8.8.8") . "\n";echo geoip_country_code_by_addr($gi, "61.139.2.69") . "\t" . geoip_country_name_by_addr($gi, "61.139.2.69") . "\n";geoip_close($gi);

复制代码

2,详细到国家城市

include("geoipcity。inc");include("geoipregionvars。php");$gi = geoip_open("。/GeoLiteCity。dat",GEOIP_STANDARD); $record = geoip_record_by_addr($gi,"8。8。8。8");print $record->country_code 。 " " 。 $record->country_code3 。 " " 。 $record->country_name 。 "\n";print $record->region 。 " " 。 $GEOIP_REGION_NAME[$record->country_code][$record->region] 。 "\n";print $record->city 。 "\n";print $record->postal_code 。 "\n";print $record->latitude 。 "\n";print $record->longitude 。 "\n";print $record->metro_code 。 "\n";print $record->area_code 。

"\n";print $record->continent_code 。 "\n"; print "\n-----\n";$record = geoip_record_by_addr($gi,"61。139。2。69");print $record->country_code 。 " " 。 $record->country_code3 。 " " 。 $record->country_name 。 "\n";print $record->region 。 " " 。 $GEOIP_REGION_NAME[$record->country_code][$record->region] 。 "\n";print $record->city 。 "\n";print $record->postal_code 。 "\n";print $record->latitude 。 "\n";print $record->longitude 。 "\n";print $record->metro_code 。 "\n";print $record->area_code 。 "\n";print $record->continent_code 。 "\n";geoip_close($gi);

复制代码

在使用以上代码时,可以根据开发环境与具体情况决定用哪种。

(编辑:武陵站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!