Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii check if user is logged before every page

Tags:

php

yii

I have to check if user is logged before rendering every page example:

http://mypage.com/site/about

at begining check if user is logged in, if not - redirect tom login page I don't want to add it in every single componene, how to to this?

like image 799
rafal235 Avatar asked Jun 19 '13 09:06

rafal235


2 Answers

You can also check using this if it is true then user is not logged in else logged in

    if(Yii::app()->user->isGuest){
     //not logged user
    }else{
     //loggedin user
    }
like image 52
Ninad Avatar answered Oct 15 '22 03:10

Ninad


Use access rule to achevie this would be a better way:

public function accessRules()
{
     return array(
         array('allow',  // allow all users to perform 'index' and 'contact' actions
              'actions'=>array('index','contact'),
              'users'=>array('*'),
         ),
         array('allow', // allow authenticated user to perform 'delete' and 'update' actions
              'actions'=>array('update','delete'),
              'users'=>array('@'),
         ),
         array('deny',  // deny all users
               'users'=>array('*'),
        ),
     );
}

if you really want one-place checking,,then go to component/controller and do it in the controller. because all controller inherits from that controller.

like image 21
bingjie2680 Avatar answered Oct 15 '22 03:10

bingjie2680