我正在使用 Yii 1.1.16,并尝试添加一个 accessRules
以按 IP 进行过滤
这是我在 CommentsController
中的代码。它仍然允许我的本地主机 IP 访问该操作。除了我的 Action ,这是我的 Controller 中唯一的其他功能。
我错过了什么或做错了什么?谢谢
public function accessRules() {
return array (
array ('allow', // allow all users to perform these actions
'actions' => array ( 'Comments' ),
'ips' => array(
/*"127.0.0.1",*/ /* localhost */
/*"::1",*/ /* localhost */
"52.XX.XX.XX" //live site
)
),
);
}
最佳答案
您还必须添加一个deny
:
......
public function filters()
{
return array(
'accessControl', //access control filter should be applied to every action of the controller
);
}
public function accessRules() {
return array (
array ('allow', // allow all users to perform these actions
'actions' => array ( 'comments' ),
'ips' => array("52.XX.XX.XX")
),
array ('deny', //deny by default
'ips' => array("*")
),
);
}
之所以使用 deny
规则是因为如果没有任何规则与上下文匹配,那么将执行 deny
规则。更多信息 here .
关于php - yii 通过 IP 地址访问规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33367384/