使用“记住我"自动登录不工作 [英] Auto login using "remember me" not working
问题描述
我是 Yii2 的新手.我使用的是 Yii 2 基本模板.
I am new to Yii2. I'm using Yii 2 basic template.
我在我的 Web 应用程序上实现了记住我"功能,但它并没有像我想象的那样工作.我可以成功登录(选中记住我"复选框).但是关闭浏览器并再次打开网站后,我没有登录,而是重定向到登录页面.
I have the "remember me" functionality implemented on my web application, but it is not working as I think it was supposed to. I can successfully log in (with a checked "remember me" checkbox). But after closing the browser and opening the website again I am not logged in, but instead redirected to the login page.
我已在配置文件中将 enableAutoLogin
设置为 true
I have set enableAutoLogin
to true
in the config file
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
'authTimeout' => 86400,
],
推荐答案
1. 在您的用户中执行此操作(如果使用任何其他模型进行登录,则使用该模型)模型.
1. Do this in your user (if using any other model for login then use that model) model.
在规则
public $rememberMe = true;
并在您的模型中定义
public function login()
{
if ($this->validate()) {
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
}
return false;
}
2.现在在您的视图页面中执行此操作
2. now do this in your view page
<?= $form->field($model, 'rememberMe')->checkbox(['template' => "<div class=\"col-lg-offset-1 col-lg-3\">{input} {label}</div>\n<div class=\"col-lg-8\">{error}</div>"]) ?>
这篇关于使用“记住我"自动登录不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!