Session 尽管会话超时设置为至少1天,但在用户空闲固定秒后,Yii2会话将过期
我已经在我的config/web文件中添加了这些代码Session 尽管会话超时设置为至少1天,但在用户空闲固定秒后,Yii2会话将过期,session,yii2,Session,Yii2,我已经在我的config/web文件中添加了这些代码 'user' => [ 'identityClass' => 'app\models\User', 'enableAutoLogin' => true, 'enableSession' => true, 'authTimeout' =>86400, 'loginUrl' => ['account/login'],
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
'enableSession' => true,
'authTimeout' =>86400,
'loginUrl' => ['account/login'],
],
'session' => [
'timeout' => 86400,
],
会话到期后,我想自动注销并重定向到登录操作。首先,您必须将
'enableAutoLogin'=>设置为false,
。
现在将这些行添加到config/web中。
我在frontend/config/main.php中添加了它,因为我只使用frontend
'components' => [
...
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => false,
'enableSession' => true,
'authTimeout' => 1800, //30 minutes
'identityCookie' => ['name' => '_identity-frontend', 'httpOnly' => true],
],
'session' => [
// this is the name of the session cookie used for login on the frontend
'class' => 'yii\web\Session',
'name' => 'advanced-frontend',
'timeout' => 1800,
],
...
现在转到yii2/web/User.php
,在以来宾身份返回之前,在注销方法中为销毁会话编写代码()
public function logout($destroySession = true)
{
$identity = $this->getIdentity();
if ($identity !== null && $this->beforeLogout($identity)) {
......
if ($destroySession && $this->enableSession) {
Yii::$app->getSession()->destroy();
}
$this->afterLogout($identity);
}
$session = Yii::$app->session;
$session->remove('other.id');
$session->remove('other.name');
// (or) if is optional if above won't works
unset($_SESSION['class.id']);
unset($_SESSION['class.name']);
// (or) if is optional if above won't works
unset($session['other.id']);
unset($session['other.name']);
return $this->getIsGuest();
}
对我来说,它工作得很好。确保在php.ini文件中
配置
session.gc_maxlifetime
默认设置为:
session.gc_maxlifetime=1440
将其更改为:
session.gc_maxlifetime=86400
请尝试'enableAutoLogin'=>false
。我也试过了,但还是一样,超时时间设置为一天。尝试将其设置为几秒钟以测试会话。可能存在重复的