Yii 1.1: How to limit number of guest page views

6 followers

In some cases, we have to limit the number of page views for the guests users. Yii provide an efficient method to get this.

Here is how you can do this, if your application requires this.

Open file

protected/components/Controller.php

and add the following code there in

public function beforeAction($action) {
        // allowed actions to be performed 
        $allowed=array(
            'login','register','passwordreset','updatepassword','logout'
        );
        // page views
        if(!isset(Yii::app()->session['pages'])){
            Yii::app()->session['pages']=1;
        }else{
            Yii::app()->session['pages']=Yii::app()->session['pages']+1;
        }
        $pages = Yii::app()->session['pages'];
        if(Yii::app()->user->isGuest){
            if($pages>10){
 
                if(!in_array($action->id, $allowed)){
                    $this->redirect(array('/member/login')); // redirect to login
                }
            }
        }   
        return parent::beforeAction($action);
    }

Total 1 comment

#17144 report it
Stageline at 2014/05/07 07:45am
question

And what's up when i disable cookies? :-)

Leave a comment

Please to leave your comment.

Write new article