-1

我想添加我的 Yii 项目 IP 过滤功能。下面的代码实际上向我添加了这个功能(它有帮助)。但是我想在运行时添加这个功能,在我的程序(yii 项目)运行之后,用户可能会在允许 ip 列表(白名单)中添加另一个 IP 地址,并拒绝其他一些 IP 地址进入阻止列表(如黑名单)。您能帮我了解如何将这些功能添加到我的项目中。

谢谢,从现在开始,

#in the SiteController
public function accessRules() {
        return array(
            array('allow',
            'actions' => array('index','view', 'create', 'update', 'manage'),
            'ips' => Yii::app()->params['allowIps'],//updated to pull list from Yii
           ),
            array('deny',
                'actions' => array('index','view', 'create', 'update', 'manage'),
                'ips' => array('*'),
            ),
        );
    }

在 /protected/config/main.php

    'params'=>array(
            // this is used in contact page
            'allowedIps'=>array('22.150.133.177'),
    ),
4

2 回答 2

0

The other answer will only work during a given session and then the blacklist will revert.

I would suggest you use a Security component (that you write) that stores the appropriate information in a database (or some other system that allows you to change it). Then, either have the security component modify the params array or better yet modify your system to retrieve the info directly from the component.

于 2013-11-13T16:44:05.417 回答
0

用于添加新 IP

array_push(Yii::app()->params['allowIps'],'4.4.4.4');

用于删除 IP

if (($key = array_search('4.4.4.4', Yii::app()->params['allowIps'])) !== false) {
    unset(Yii::app()->params['allowIps'][$key]);
}
于 2013-11-13T03:41:16.023 回答