php 设置白名单ip
php 设置ip白名单
//检查白名单ip
private function _checkAllowIp()
{
$allowIp = ['203.195.156.12'];
$ip = $this->getIp();
if (!in_array($ip, $allowIp)) {
exit();
}
}
/**
* 获取客户端访问ip
*/
private function getIp()
{
if (getenv('HTTP_CLIENT_IP')) {
$ip = getenv('HTTP_CLIENT_IP');
} else if (getenv('HTTP_X_FORWARDED_FOR')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
} else if (getenv('REMOTE_ADDR')) {
$ip = getenv('REMOTE_ADDR');
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
$ips = explode(',', $ip);
if (count($ips) > 1) {
$ip = $ips[0];
}
return $ip;
}
正因为来之不易,所以才有了后来的倍加珍惜。