php使用GeoIP库实例
需要注意的是maxmind是geoip数据库的提供方, 同时也提供了很多语言的sample和api说明文档。 比如php,和php的geoip库是有很大区别的,包含使用方式,接口函数。 php官方的geoip需要配置php环境,加载geoip.dll库,并在php.ini中指定GEOIP库地址。 maxmind提供一系列 .inc 和 .php 对环境无依赖,只要支持php,直接require后即可使用。 一、GEOIP数据库http://dev.maxmind.com/geoip/geolite 细分到国家:GeoLite Country 细分到城市:GeoLite City二、php官方geoip.dll库下载dll http://windows.php.net/downloads/pecl/releases/geoip/1.0.8/ 修改php.ini,启用geoip库: 代码如下:
追加geoip段,指定数据库位置:
代码如下: 测试代码 注意geoip.dll中使用的 GeoIPCity.dat 即 GeoLiteCity.dat,用的时候注意看提示 代码如下: 三、MaxMind官方php文件函数库文档和示例:http://dev.maxmind.com/geoip/downloadable 修改maxmind示例中 sample.php 和 sample_city.php 中 GeoIP.dat/GeoLiteCity.dat 路径为你自己的路径 同一目录用 “./GeoIP.dat” 或 “./GeoLiteCity.dat” 即可。 详细到国家 代码如下: 详细到国家城市 代码如下: $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"; 看自己开发环境和具体情况决定用哪种 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |