authTimeout in Yii2

2019-02-17 10:38发布

I am trying to log out user automatically in yii2 after he is idle for a fixed seconds . In web.php I added

'user' => [
            'identityClass' => 'app\models\User',
            'enableAutoLogin' => true,
            'authTimeout'=>100
        ],

inside components . I am using basic template. But it is not logging out automatically. Does this work in Yii2? I was following documentation from http://www.yiiframework.com/doc-2.0/yii-web-user.html

6条回答
Explosion°爆炸
2楼-- · 2019-02-17 10:47

Just remove "enableAutoLogin" from your user config and it will just work fine.

Your code shall look like this:

'user' => [
            'identityClass' => 'app\models\User',
            'authTimeout'=>100
        ],
查看更多
我想做一个坏孩纸
3楼-- · 2019-02-17 10:48

In your config/web.php file:

In $config array:

...

'user' => [
            'identityClass' => 'app\models\User',
            //'enableAutoLogin' => true,
            'enableSession' => true,
            'authTimeout' => 60,
        ],

...

Please note I commented the //enableAutoLogin, that prevents of authTimeout to work properly

查看更多
叼着烟拽天下
4楼-- · 2019-02-17 11:00

Your config is correct. But it will not automatically refresh your page and show you login form. Technically it will log you out only at the next request after the session is expired. And you should be aware of ajax scripts working on your page and calling some other pages by time interval. Every request will renew your session timeout. There is also "absoluteAuthTimeout" parameter instead of "authTimeout" - that will log you out after the timeout despite of your activity.

查看更多
男人必须洒脱
5楼-- · 2019-02-17 11:01

$authTimeout - public property.

The number of seconds in which the user will be logged out automatically if he remains inactive. If this property is not set, the user will be logged out after the current session expires (c.f. yii\web\Session::$timeout).

Note that this will not work if $enableAutoLogin is true.

查看更多
Luminary・发光体
6楼-- · 2019-02-17 11:08

if you want log out user after X time. You should check with ajax each second. If expired redirect to log out

查看更多
SAY GOODBYE
7楼-- · 2019-02-17 11:10

You can also use

session.gc_maxlifetime

setting in php.ini

By default it is 1440 secs.

查看更多
登录 后发表回答