我已在配置/网络文件中添加了这些代码
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
'enableSession' => true,
'authTimeout' =>86400,
'loginUrl' => ['account/login'],
],
'session' => [
'timeout' => 86400,
],
会话过期后,我想自动注销并重定向到登录操作。
答案 0 :(得分:1)
确保在您的php.ini文件中
session.gc_maxlifetime
默认设置为:
session.gc_maxlifetime=1440
将其更改为:
session.gc_maxlifetime=86400
答案 1 :(得分:0)
首先,您必须设置'enableAutoLogin' => false,
。
现在在配置/网络中添加这些行。
我在frontend/config/main.php
添加了它,因为我只使用前端。
'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
并在logout method before return as guest()
-
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();
}
对我而言,它很有效。