// open the geoip database $gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);
// 获取国家代码 $country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']); echo "Your country code is: <strong>$country_code</strong> <br />";
// 获取国家名称 $country_name = geoip_country_name_by_addr($gi, $_SERVER['REMOTE_ADDR']); echo "Your country name is: <strong>$country_name</strong> <br />";
1、查找国内的ip段
2、获取当前访问的用户主机(ip地址)与国内ip比较进行条件判断处理是吗?
GeoIP数据库是广泛使用的商业IP数据库,GeoIP数据库免费版提供国家代码的信息,也就是说能够精确到IP地址所在地国家,如果你要更精确的IP地址定位,那么你需要购买更高版本。这里给出一个GeoIP Lite版本的下载地址和简单应用。GeoIP 数据库文件下载:
http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP....GeoIP的PHP API文件打包下载地址:
http://geolite.maxmind.com/download/geoip/api/php/php-1.11.tar.gzGeoIP查询PHP脚本:下面的代码能够给出访问者IP的国家代码与国家名称<?php
//计时开始
function utime() {
$time = explode( " ", microtime() );
$usec = (double)$time[0];
$sec = (double)$time[1];
return $usec + $sec;
}
$startTimes = utime();
include("geoip.inc");
// open the geoip database
$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);
// 获取国家代码
$country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
echo "Your country code is: <strong>$country_code</strong> <br />";
// 获取国家名称
$country_name = geoip_country_name_by_addr($gi, $_SERVER['REMOTE_ADDR']);
echo "Your country name is: <strong>$country_name</strong> <br />";
// close the database
geoip_close($gi);
//运行结束时间
$endTimes = utime();
$runTimes = sprintf( '%0.4f', ( $endTimes - $startTimes ) );
echo "Processed in " . $runTimes . "second.";
?>