接口文檔地址:http://ip.taobao.com/instructions.html
$ip=get_client_ip();
if(!preg_match("/^[\d]+\.[\d]+\.[\d]+\.[\d]+$/isU",$ip)){
echo "IP地址錯誤";
}else{
//淘寶IP接口
$url='http://ip.taobao.com/service/getIpInfo.php?ip='.$ip;
$result = file_get_contents($url);
$result = json_decode($result,true);
if($result['code']!==0 || !is_array($result['data'])){
echo "IP地址錯誤";
}else{
print_r($result);
}
}
/**
* 獲取客戶端IP地址
* @param integer $type 返回類型 0 返回IP地址 1 返回IPV4地址數字
* @param boolean $adv 是否進行高級模式獲取(有可能被僞裝)
* @return mixed
*/
function get_client_ip($type = 0,$adv=true) {
$type = $type ? 1 : 0;
static $ip = NULL;
if ($ip !== NULL) return $ip[$type];
if($adv){
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
$pos = array_search('unknown',$arr);
if(false !== $pos) unset($arr[$pos]);
$ip = trim($arr[0]);
}elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
}elseif (isset($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR'];
}
}elseif (isset($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR'];
}
// IP地址合法驗證
$long = sprintf("%u",ip2long($ip));
$ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
return $ip[$type];
}