代码很简洁,功能很实用,这里就不多废话了,直接奉上:
<br /><?php<br />/**<br /> * 获取客户端IP<br /> * @param integer $type 返回类型 0:string,1:long<br /> * @return string|long<br /> */<br />function getClientIp($type = 0) {<br /> $ip = NULL;<br /> if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {<br /> $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);<br /> $pos = array_search('unknown',$arr);<br /> if(false !== $pos) unset($arr[$pos]);<br /> $ip = trim($arr[0]);<br /> }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {<br /> $ip = $_SERVER['HTTP_CLIENT_IP'];<br /> }elseif (isset($_SERVER['REMOTE_ADDR'])) {<br /> $ip = $_SERVER['REMOTE_ADDR'];<br /> }<br /> $long = sprintf("%u", ip2long($ip));<br /> $ip = $long ? array($ip, $long) : array('0.0.0.0', 0);<br /> return $ip[$type];<br />}<br />/**<br /> * 获取IP信息<br /> * @param string|long $ip IP地址<br /> * @return array<br /> */<br />function getIpInfo($ip)<br />{<br /> if (is_long($ip)) {<br /> $ip = long2ip($ip);<br /> }<br /> $api = 'http://ip.taobao.com/service/getIpInfo.php';<br /> $ret<i style="color:transparent">本#文来源gaodai$ma#com搞$$代**码网$</i><button>搞代gaodaima码</button> = file_get_contents($api . '?ip=' . $ip);<br /> $ret = json_decode($ret, true);<br /> if ($ret['code'] == 0) {<br /> return $ret['data'];<br /> } <br /> return array();<br />}<br />
以上就是本文的全部内容了,希望大家能够喜欢。