请记住,基本上所有referer_check都是这样的:
$pattern = "/^http:\/\/www\.myurl\.com(\/.*)*$/";
if(!empty($_SERVER['HTTP_REFERER']) && !preg_match($pattern, $_SERVER['HTTP_REFERER'])) {
session_destroy();
}
令人讨厌的是,内置的PHP不会接受一组URL,但您始终可以制作自己的URL数组。referer_check
因此,对于CakePHP,您可以执行以下操作:
// ADD THIS TO /app/config/config.php
$config['CustomSecurity'] = array(
'accept_referers' => array(
'http://www.my_site.com',
'https://www.other_allowed_referer.com',
)
);
// ADD THIS TO /app/app_controller.php
private function referer_check(){
if(!empty($_SERVER['HTTP_REFERER'])) {
$accept_referers = Configure::read('CustomSecurity.accept_referers');
$referer_accepted = false;
foreach($accept_referers as $referer) {
$pattern = '/^'.preg_replace('/(\.|\/)/','\\\$1',$referer).'(\/.*)*$/';
if(preg_match($pattern, $_SERVER['HTTP_REFERER']))
$referer_accepted = true;
}
if(!$referer_accepted) {
$this->Session->destroy();
exit;
}
}
}
在您的函数中,调用:app_controller::before_filter
$this->referer_check();
...或者类似的东西...很抱歉代码格式,文本区域是一个迟到的:)