php使用GeoIP庫實例
需要注意的是maxmind是geoip數(shù)據(jù)庫的提供方,
同時也提供了很多語言的sample和api說明文檔。
比如php,和php的geoip庫是有很大區(qū)別的,包含使用方式,接口函數(shù)。
php官方的geoip需要配置php環(huán)境,加載geoip.dll庫,并在php.ini中指定GEOIP庫地址。
maxmind提供一系列 .inc 和 .php 對環(huán)境無依賴,只要支持php,直接require后即可使用。
一、GEOIP數(shù)據(jù)庫
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段,指定數(shù)據(jù)庫位置:
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文件函數(shù)庫
文檔和示例:http://dev.maxmind.com/geoip/downloadable
修改maxmind示例中 sample.php 和 sample_city.php 中 GeoIP.dat/GeoLiteCity.dat 路徑為你自己的路徑
同一目錄用 “./GeoIP.dat” 或 “./GeoLiteCity.dat” 即可。
詳細到國家
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);
詳細到國家城市
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);
看自己開發(fā)環(huán)境和具體情況決定用哪種
相關(guān)文章
TP5(thinkPHP5)框架使用ajax實現(xiàn)與后臺數(shù)據(jù)交互的方法小結(jié)
這篇文章主要介紹了TP5(thinkPHP5)框架使用ajax實現(xiàn)與后臺數(shù)據(jù)交互的方法,結(jié)合實例形式總結(jié)分析了thinkPHP5使用ajax與后臺數(shù)據(jù)交互的兩種實現(xiàn)方法及相關(guān)操作技巧,需要的朋友可以參考下2020-02-02PHP中使用GD庫繪制折線圖 折線統(tǒng)計圖的繪制方法
本文通過代碼給大家介紹php中使用GD庫繪制折線圖,涉及到php中GD庫的一些簡單使用,本文介紹的非常詳細,感興趣的朋友一起學習吧2015-11-11laravel 實現(xiàn)劃分admin和home 模塊分組
今天小編就為大家分享一篇laravel 實現(xiàn)劃分admin和home 模塊分組,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10laravel yajra插件 datatable的使用詳解
這篇文章主要介紹了laravel yajra插件 datatable的使用詳解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01