获取客户端的ip
2017-02-16 14:18:58
/**
* 获取客户端的ip
*
* @param boolean $format
* - 返回格式 0 : 原格式 1:数字
* @return string/int
*/
function getIp($format = 0)
{
if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
$ip = getenv('HTTP_CLIENT_IP');
} elseif (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
} elseif (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
$ip = getenv('REMOTE_ADDR');
} elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
$ip = $_SERVER['REMOTE_ADDR'];
}
preg_match('/[d.]{7,15}/', $ip, $ipmatches);
$onlineip = $ipmatches[0] ? $ipmatches[0] : '0.0.0.0';
if ($format) {
return ip2long($onlineip);
} else {
return $onlineip;
}
}
发表评论: