/**
* 获取客户端IP
*/
function getIp() {
$ip_array = array ();
if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] != '') {
$ip_array[] = $_SERVER['REMOTE_ADDR'];
}
if (isset ($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] != '') {
$ip_array[] = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
if (count($ip_array) <= 1)
return $ip_array[0];
$ip_private_list = array (
'10.0.0.0/8',
'172.16.0.0/12',
'192.168.0.0/16'
);
$ip = 'unknown';
foreach ($ip_array as $ip_s) {
if ($ip_s != "" AND !isIpInNetArray($ip_s, $ip_private_list)) {
$ip = $ip_s;
break;
}
}
return $ip;
}
function isIpInNet($ip, $net, $mask) {
$lnet = ip2long($net);
$lip = ip2long($ip);
$binnet = str_pad(decbin($lnet), 32, "0", "STR_PAD_LEFT");
$firstpart = substr($binnet, 0, $mask);
$binip = str_pad(decbin($lip), 32, "0", "STR_PAD_LEFT");
$firstip = substr($binip, 0, $mask);
return (strcmp($firstpart, $firstip) == 0);
}
function isIpInNetArray($theip, $thearray) {
$exit_c = false;
foreach ($thearray as $subnet) {
list ($net, $mask) = split("/", $subnet);
if (isIpInNet($theip, $net, $mask)) {
$exit_c = true;
break;
}
}
return $exit_c;
}