Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yii accessRules by IP address

Tags:

php

yii

I'm using Yii 1.1.16, and am trying to add an accessRules to filter by IP

This is my code in my CommentsController. It still allows my localhost IP to access the action. Other than my actions, this is the only other function in my controller.

What am i missing or doing wrong? Thanks

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
            )
        ),
    );
}
like image 605
user2636556 Avatar asked Dec 01 '25 15:12

user2636556


2 Answers

You'll have to add a deny too:

......
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("*")
            ),
    );
}

The reason for having a deny rule is because if none of the rules matches a context, then the deny rule gets executed. More info here.

like image 196
Criesto Avatar answered Dec 04 '25 06:12

Criesto


You can try following code.

//controller file.

public function filters()
{
    return array( 'accessControl' ); // perform access control for CRUD operations
}

public function accessRules() {
    return array (
        array ('allow', // allow all users to perform these actions
            'actions' => array ( 'Comments' ),
            'ips' => array(
                "52.XX.XX.XX" //live site
            )
        ),
        array ('deny', // deny all users to perform these actions
            'actions' => array ( 'Comments' ),
            'ips' => array('*')
        ),
    );
}
like image 42
GAMITG Avatar answered Dec 04 '25 06:12

GAMITG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!