CakePHP2でコントローラーでIPアドレスの制限をかけたい場合。
public function beforeFilter() {
//許可されたIP意外はアクセスさせない
$ipList = ['192.168.0.1','123.456.789.123'];
$ip = $this->request->clientIP();
//許可されたIPかどうか
if(!in_array($ip, $ipList)){
$this->log('許可されていないIP:'.$ip);
throw new ForbiddenException();
}
parent::beforeFilter();
}
社内の場合は~って場合は、preg_match使うのもいいかも。
public function beforeFilter() {
//許可されたIP意外はアクセスさせない
$ipList = ['192.168.0.1','xxx.xxx.xxx.xxx'];
$ip = $this->request->clientIP();
//社内判定
if (!preg_match("/192\.168\.0\..*/",$ip)){
//許可されたIPかどうか
if(!in_array($ip, $ipList)){
$this->log('許可されていないIP:'.$ip);
throw new ForbiddenException();
}
}
parent::beforeFilter();
}