php Yii 检查用户是否在每个页面之前登录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17187334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Yii check if user is logged before every page
提问by rafal235
I have to check if user is logged before rendering every page example:
我必须在呈现每个页面示例之前检查用户是否已登录:
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?
在开始时检查用户是否已登录,如果没有 - 重定向登录页面我不想将它添加到每个组件中,如何做到这一点?
采纳答案by bingjie2680
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.
如果你真的想要一个地方检查,然后去component/controller
在控制器中进行。因为所有控制器都继承自该控制器。
回答by Ninad
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
}
回答by Ankur Garg
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
你只需要在component/Controller.php中添加上面的函数
回答by Khawer Zeshan
You can write a check in the init()
function of the controller. Which will redirect the user if he is not logged in
您可以init()
在控制器的功能中写一个检查。如果用户未登录,这将重定向用户
public function init()
{
if(!isset(Yii::app()->session['user']))
{
$this->redirect(array('login/'));
}
}
回答by topher
For a global solution add accessControl to your base controller (by default protected/components/CController.php
).
对于全局解决方案,将 accessControl 添加到您的基本控制器(默认情况下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
然后在控制器中使用您的登录操作编辑accessRules
以允许所有用户访问登录页面
public function accessRules()
{
return array_merge(array(
'allow',
'actions'=>array('login'),
'users'=>array('*'),
),parent::accessRules()
);
}
回答by Oleksandr Knyga
Extend components/Controller with beforeAction
使用 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;
}
回答by Ruben
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
http://stuff.cebe.cc/yii2docs/guide-concept-configurations.html#configuration-format
回答by Keenan Diggs
Sorry for zombie posting, but I use isGuest.
对不起,僵尸发帖,但我使用 isGuest。
if (Yii::app()->user->isGuest)
{
$this->redirect('login/page');
}
回答by P Ravikant
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.
然后在每个文件中包含该 php 页面。
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.
在 checklogin.php 页面中,您可以编写以下内容来检查是否设置了 cookie。
isset(cookie('<name_of_cookie>'))
{
//User in already logged in
}
else
{
//Redirect to login page
}