Yii 会话超时,但一些控制器仍然可以访问。如何将用户发送到登录页面

Yii session timed out but some controllers are still accessible. How to send the user to the login page

我使用的是 Yii 1.10,但会话有问题。如果登录的用户离开他的系统一段时间,Yii 检测到并销毁会话, 在这种情况下,用户不应该能够访问任何控制器或任何控制器的任何操作。 但在我的情况下,一些控制器仍然可以访问,并且它们正在向我显示结果。这不好。 请建议我应该怎么做才能防止访问,我做错了什么请帮助我摆脱它。

如果你想用 yii 的方式来做,你应该使用 access control filter。它基本上检查当前用户是否有权访问请求的控制器操作。

对于简单的用例来说应该足够了。对于复杂的情况,您应该使用 RBAC。您可以获得更多关于如何在 yii 中使用 RBAC 的详细信息 here

你可以这样做:

在你的Module.php

public function beforeControllerAction($controller, $action) {
        if (parent::beforeControllerAction($controller, $action)) {
            $controller->layout = 'admin_dashboard';
            if(empty(Yii::app()->session['admin_id']) || empty($_SESSION['admin_id'])){
                unset($_SESSION);
                Yii::app()->user->logout();
            }
            $route = strtolower ($controller->id . '/' . $action->id);
            //Add those page which not requires authentication like: 'action/controller',
            $publicPages = array(

            );
            if (Yii::app()->user->isGuest && !in_array($route, $publicPages)){
                Yii::app()->getModule('admin')->user->loginRequired();
            }
            else
            {
                return true;
            }
        } else
            return false;
    }