Мне пришлось поделиться своим кодом для этого, если вы находитесь за CDN и хотите использовать разрешение нескольких IPS:
/**
* return array value in key in case it exists and has value
* @param array $arr
* @param $key
* @return bool|mixed
*/
function is($arr = array(), $key){
if(isset($arr[$key]) && $arr[$key]){
return $arr[$key];
}
return false;
}
/**
* this function return the actual client IP in case it is behind CDN
* @return string
*/
function getClientIp() {
$ipAddress = '';
if (is($_SERVER, 'HTTP_CLIENT_IP')) {
$ipAddress = $_SERVER['HTTP_CLIENT_IP'];
} else if (is($_SERVER, 'HTTP_X_FORWARDED_FOR')) {
$ipAddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else if (is($_SERVER, 'HTTP_X_FORWARDED')) {
$ipAddress = $_SERVER['HTTP_X_FORWARDED'];
} else if (is($_SERVER, 'HTTP_FORWARDED_FOR')) {
$ipAddress = $_SERVER['HTTP_FORWARDED_FOR'];
} else if (is($_SERVER, 'HTTP_FORWARDED')) {
$ipAddress = $_SERVER['HTTP_FORWARDED'];
} else if (is($_SERVER, 'REMOTE_ADDR')) {
$ipAddress = $_SERVER['REMOTE_ADDR'];
} else {
$ipAddress = 'UNKNOWN';
}
return $ipAddress;
}
/**
* Allowed IPS
*/
$allowedIPSArr = array(
'127.0.0.1',
'127.0.0.2',
);
if(!in_array(getClientIp(),$allowedIPSArr, true)){
// You might want to do some redirect here
die();
}