class ip {
/**
* 取ip
* @return string
*/
public static function get() {
if ($_server['http_client_ip'] && $_server
['http_client_ip']!='unknown') {
$ip = $_server['http_client_ip'];
} elseif ($_server['http_x_forwarded_for'] && $_server
['http_x_forwarded_for']!='unknown') {
$ip = $_server['http_x_forwarded_for'];
} else {
$ip = $_server['remote_addr'];
}
return $ip;
}
/**
* ip转成整形数值
* @param string $ip ip
* @return int
*/
public static function iptoint($ip) {
$ips = explode('.',$ip);
if (count($ips)>=4) {
$int = $ips[0]*256*256*256+$ips[1]*256*256+$ips[2]
*256+$ips[3];//根据ip,a,b,c类进行计算
} else {
throw new exception('ip is error');
}
return $int;
}
/**
* 判断ip是否在一个ip段内
* @param string $startip 开始ip
* @param string $endip 结束ip
* @param string $ip ip
* @return bool
*/
public static function isin($startip, $endip, $ip) {
$start = ip::iptoint($startip);
$end = ip::iptoint($endip);
$ipint = ip::iptoint($ip);
$result = false;
if ($ipint>=$start && $ipint $result = true;
}
return $result;
}
}
