PHP根据访问用户IP判断所属区域信息
PS:原创文章,如需转载,请注明出处,谢谢!本文地址:http://flyer0126.iteye.com/blog/1583082
/**
* 根据访问用户IP判断所属区域信息
*
* @author flyer0126
* @since2012/07/09
*/
/**
* 获取用户端ip
* @return Ambigous <unknown, string>
*/
function getIP(){
if (isset($_SERVER)) {
if (isset($_SERVER)) {
$realip = $_SERVER;
} elseif (isset($_SERVER)) {
$realip = $_SERVER;
} else {
$realip = $_SERVER;
}
} else {
if (getenv("HTTP_X_FORWARDED_FOR")) {
$realip = getenv( "HTTP_X_FORWARDED_FOR");
} elseif (getenv("HTTP_CLIENT_IP")) {
$realip = getenv("HTTP_CLIENT_IP");
} else {
$realip = getenv("REMOTE_ADDR");
}
}
return $realip;
}
$ip = getIP();
// 利用新浪接口根据ip查询所在区域信息
$res0 = file_get_contents("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=$ip");
$res0 = json_decode($res0);
print_r($res0);
// 利用淘宝接口根据ip查询所在区域信息
$res1 = file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=$ip");
$res1 = json_decode($res1);
print_r($res1);
示例输出结果如下:
stdClass Object
(
=> 1
=> XXX.XXX.XXX.XXX
=> XXX.XXX.XXX.XXX
=> 中国
=> 北京
=> 北京
=>
=> 电信
=> 机房
=> 南三环洋桥电信机房
)
stdClass Object
(
=> 0
=> stdClass Object
(
=> 中国
=> 86
=> 华北
=> 100000
=> 北京市
=> 110000
=> 北京市
=> 110000
=>
=> -1
=> 电信
=> 100017
=> XXX.XXX.XXX.XXX
)
)
页:
[1]