1. 程式人生 > >根據ip地址獲取所在省市城市地區的真實地址/php函式

根據ip地址獲取所在省市城市地區的真實地址/php函式

/*獲取客戶端ip*/

function _get_ip(){

		if (isset($_SERVER['HTTP_CLIENT_IP']) && strcasecmp($_SERVER['HTTP_CLIENT_IP'], "unknown")) 

			$ip = $_SERVER['HTTP_CLIENT_IP']; 

		else if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'], "unknown")) 

			$ip = $_SERVER['HTTP_X_FORWARDED_FOR']; 

		else if (isset($_SERVER['REMOTE_ADDR']) && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) 

			$ip = $_SERVER['REMOTE_ADDR']; 

		else if (isset($_SERVER['REMOTE_ADDR']) && isset($_SERVER['REMOTE_ADDR']) && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) 

			$ip = $_SERVER['REMOTE_ADDR']; 

		else $ip = ""; 

		return ($ip);

}





/* 利用淘寶的ip地址庫獲獲取ip + 地址*/

function _get_ip_dizhi(){

		$opts = array(

			'http'=>array(

			'method'=>"GET",

			'timeout'=>5,)

		);		

		$context = stream_context_create($opts); 

		$ipmac=_get_ip();

		if(strpos($ipmac,"127.0.0.") === true)return '';

		$url_ip='http://ip.taobao.com/service/getIpInfo.php?ip='.$ipmac;

		$str = @file_get_contents($url_ip, false, $context);

		if(!$str) return "";

		$json=json_decode($str,true);

		if($json['code']==0){

			$ipcity= $json['data']['region'].$json['data']['city'];

			$ip= $ipcity.','.$ipmac;

		}else{

			$ip="";

		}

		return $ip;

}

//用法
//$ip = _get_ip_dizhi();  
//echo $ip;