简体   繁体   English

Session Yii2 超时

[英]Session timeout in Yii2

I have used built in yii2 function to set session. I am not able to use built in yii2 login because of some requirement.我已经使用内置 yii2 function 来设置 session。由于某些要求,我无法使用内置 yii2 登录。

So I have set session using below:所以我使用下面的方法设置了 session:

Yii::$app->session->set('unique_code', 'xxxx'); Yii::$app->session->set('unique_code', 'xxxx');

and in my config/main.php file在我的 config/main.php 文件中

'session' => [
        // this is the name of the session cookie used for login on the frontend
        'name' => 'project-frontend',
        'timeout' => 60*60*24*30,
    ],

But still user is logged out from website after some time.但是一段时间后用户仍然从网站注销。

So how to increase session timeout in this case?那么在这种情况下如何增加 session 超时呢?

I will suggest you that if you want to destroy particular session only then set two session: 我建议您,如果只想破坏特定的会话,则设置两个会话:

Yii::$app->session->set('unique_code', 'xxxx');
Yii::$app->session->set('code_time', 'xxxx');

Check current time with code_time, once it is over unset both session. 一旦结束,请使用code_time检查当前时间。

if you want user should logout after certain time with current solution : 如果您希望用户在一定时间后使用当前解决方案注销:

'user' => [
        'identityClass' => 'common\models\User',
        'enableAutoLogin' => false,
        'authTimeout' => 3600, // auth expire 
    ],'session' => [
        'class' => 'yii\web\Session',
        'cookieParams' => ['httponly' => true, 'lifetime' => 3600 * 4],
        'timeout' => 3600*4, //session expire
        'useCookies' => true,
    ],

Problem is in cookies expire time. 问题在于Cookie的过期时间。 When it timed out, user is logout. 超时时,用户正在注销。 Solution, for changing cookies expire time is in configuration for session component set for cookies lifetime: 解决方案,要更改Cookie的过期时间,请在Cookie生存期的会话组件集中配置:

    'components' => [
       'session' => [
            'class' => 'yii\web\Session',
            'cookieParams' => ['lifetime' => 7 * 24 *60 * 60]
       ],
   ]

When user login, cookies expire time in current example is after week. 当用户登录时,cookie在当前示例中的到期时间是一周之后。

En Mysql恩 Mysql

CREATE TABLE YiiSession
(
    id CHAR(40) NOT NULL PRIMARY KEY,
    expire INTEGER(11) NOT NULL,
    data BLOB
);

luego en web/config.php luego en web/config.php

'components' => [
    'session' => [
        'timeout' => 1440, //acá colocas el tiempo en segundos
        'class' => 'yii\web\DbSession',
        'sessionTable' => 'YiiSession',
    ],
    'user' => [
        'identityClass' => 'app\models\User',
        'enableAutoLogin' => false,
    ],
],

Lo puedes encontrar acá: http://www.bsourcecode.com/yiiframework2/session-handling-in-yii-framework-2-0/ Lo puedes encontrar acá: http://www.bsourcecode.com/yiiframework2/session-handling-in-yii-framework-2-0/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM