Home » Php » php – Yii check if user is logged before every page

php – Yii check if user is logged before every page

Posted by: admin July 12, 2020 Leave a comment

Questions:

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?

How to&Answers:

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.

Answer:

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
    }

Answer:

You can write a check in the init() function of the controller. Which will redirect the user if he is not logged in

public function init()
{
    if(!isset(Yii::app()->session['user']))
    {
        $this->redirect(array('login/'));
    }
}

Answer:

This works for me

public function beforeAction(CAction $action)
{
    if(!isset(Yii::app()->user->user_id) && !($action->controller->id == 'site' && $action->id == 'login'))
    {
        $this->redirect(array('site/login'));
    }

    return true;
}

You need to just add the above function in component/Controller.php

Answer:

For a global solution add accessControl to your base controller (by default protected/components/CController.php).

public function filters(){
    return array('accessControl');
}

public function accessRules()
{
    return array(
        array('allow',
            'users'=>array('@'),
        ),
        array('deny',  // deny all users
            'users'=>array('*'),
        ),
    );
}

Then in the controller with your login action edit the accessRules to allow all users to access the login page

public function accessRules()
{
    return array_merge(array(
            'allow',
            'actions'=>array('login'),
            'users'=>array('*'),
        ),parent::accessRules()
    );
}

Answer:

Extend components/Controller with beforeAction

public function beforeAction(CAction $action)
{
    if(!isset(Yii::app()->session['user']) && !($action->controller->id == 'site' && $action->id == 'login'))
    {
        $this->redirect(array('site/login'));
    }

    return true;
}

Answer:

you can add global behavior to your config:

'as access' => [
    'class' => \yii\filters\AccessControl::className(),
    'rules' => [
        [
            'actions' => ['login', 'error', 'resend', 'forgot'],
            'allow' => true,
        ],
        // allow authenticated users
        [
            'allow' => true,
            'roles' => ['@'],
        ],
    ]
],

http://stuff.cebe.cc/yii2docs/guide-concept-configurations.html#configuration-format

Answer:

Sorry for zombie posting, but I use isGuest.

if (Yii::app()->user->isGuest)
{
  $this->redirect('login/page');
}

Answer:

Write a code to check if the user is logged in or not in a different file.

Then include that php page in every file.

You will just have to write the following code.

include('checklogin.php');

In the checklogin.php page, you may write the following to check if the cookie is set.

isset(cookie('<name_of_cookie>'))
{
 //User in already logged in
}
else
{
 //Redirect to login page
}