php 尽管会话超时设置为至少 1 天,但 Yii 用户仍会在 15-30 分钟后注销

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12859333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:21:30  来源:igfitidea点击:

Yii users being logged out after 15-30 minutes despite session timeouts being set to at least 1 day

phpsessioncookiesyiisession-timeout

提问by Tom Busby

I've included the relevent parts of our Yii config file below:

我在下面包含了 Yii 配置文件的相关部分:

return array(
...
    'components'=>array(
        'session' => array(
            'timeout' => 86400,
        ),
        'user'=>array(
            'allowAutoLogin' => true,
            'autoRenewCookie' => true,
            'authTimeout' => 31557600,
        ),
    ...
    ),
...
);

I have also been into php.ini and set session.gc_maxlifetime = 86400but this still hasn't fixed the problem.

我也进入了 php.ini 并进行了设置,session.gc_maxlifetime = 86400但这仍然没有解决问题。

Currently, Im absolutely at a loss as to what else could be causing it to timeout and log the user out after roughly 15-30 minutes of inactivity. Ideally users should remain logged in for at least a day of inactivity (and beyond closing the browser window, browser preferences allowing).

目前,我完全不知道还有什么可能导致它超时并在大约 15-30 分钟不活动后将用户注销。理想情况下,用户应该保持登录状态至少一天不活动(除了关闭浏览器窗口,浏览器首选项允许)。

I've trawled google, Yii and stack overflow and just can't find anything that I'm overlooking... but clearly I am overlooking something. If anyone can help me out I'd be very grateful.

我已经搜索了 google、Yii 和堆栈溢出,但找不到任何我忽略的东西……但显然我忽略了一些东西。如果有人可以帮助我,我将不胜感激。



A sample of typical code that we are using to log in the users was requested and is included below:

我们用于登录用户的典型代码示例被请求,如下所示:

$identity = new UserIdentity('facebook', $id, $user->name, $user->email);
$loggedIn = Yii::app()->user->login($identity);
$this->subscriptionChecker->updateCurrentUserSubscribed();

This is pretty typical of any time that Yii::app()->user->login()is called

这是非常典型的任何时间Yii::app()->user->login()被称为



From Chrome, here are the cookies I have for the site and their expiries (after clearing all cookies and just logging in):

在 Chrome 中,以下是我为该网站准备的 cookie 及其过期时间(清除所有 cookie 并登录后):

PHPSESSID expires When the browsing session ends

// I'm informed these are set by google analytics  
__utma created Friday, 12 October 2012 14:05:31 expires Sunday, 12 October 2014 14:05:31

__utmb created Friday 12 October 2012 14:05:31 expires Friday 12 October 2012 14:35:31,

__utmc created Friday, 12 October 2012 14:05:31 expires When the browsing session ends

__utmz created Friday 12 October 2012 14:05:31 expires Saturday 13 April 2013 02:05:31  
// end google analytics

采纳答案by Tom Busby

http://www.yiiframework.com/doc/api/1.1/CWebUser#login-detail

http://www.yiiframework.com/doc/api/1.1/CWebUser#login-detail

Thanks to help from Arfeen who pointed me in the right direction, unless you set the second parameter of Yii::app()->user->login()it turns out that Yii will not use a persistent cookie, as the second parameter defaults to 0. This default 0-value overrides anything else you might have set to do with timeouts.

感谢 Arfeen 的帮助,他为我指明了正确的方向,除非你设置了第二个参数,否则Yii::app()->user->login()Yii 不会使用持久性 cookie,因为第二个参数默认为 0。这个默认的 0 值会覆盖你可能会做的任何其他事情已经设置为超时。

回答by Fernando Carvalho

I had a identical problem, even if i make authTimeout 3600 * 24 ( 24 hours ) the user still making logout in about 30 minutes. I discovered that on php.ini there is a option:

我遇到了同样的问题,即使我将 authTimeout 设为 3600 * 24(24 小时),用户仍然会在大约 30 分钟内注销。我发现在 php.ini 上有一个选项:

session.gc_maxlifetime

session.gc_maxlifetime

for default this options is 24 minutes, so i changed for what i needed

默认情况下,此选项为 24 分钟,因此我根据需要进行了更改

session.gc_maxlifetime = 86400

session.gc_maxlifetime = 86400

24 hours. Problem Solved for me.

24小时。问题为我解决。

Hope this could help someone!

希望这可以帮助某人!

回答by Muhiddin Jumaniyazov

Try this: first one when you got login you could set setStatethis:

试试这个:第一个当你登录时你可以设置setState

yii::app()->user->setState('userSessionTimeout', time() + Yii::app()->params['sessionTimeoutSeconds']); 

add those are text companents.controller.php

添加那些是文本 companents.controller.php

 public function beforeAction(){
            // Check only when the user is logged in
            if ( !Yii::app()->user->isGuest)  {
               if ( yii::app()->user->getState('userSessionTimeout') < time() ) {
                   // timeout
                   Yii::app()->user->logout();
                   $this->redirect(array('/site/login'));  //
               } else {
                   yii::app()->user->setState('userSessionTimeout', time() + Yii::app()->params['sessionTimeoutSeconds']) ;
                   return true; 
               }
            } else {
                return true;
            }
        }

and add those are in config main.php file:

并将它们添加到配置 main.php 文件中:

'params'=>array( 'sessionTimeoutSeconds'=>1800, // 30 minute ),

'params'=>array( 'sessionTimeoutSeconds'=>1800, // 30 分钟 ),

回答by uldis

For Yii2

对于 Yii2

This solution after login for session cookies set expire time after 7 days:

此解决方案在登录会话 cookie 后设置过期时间为 7 天后:

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

回答by Gajen Sunthara

For Yii2 version

对于 Yii2 版本

In your /config/params.php set the timeout in seconds:

在您的 /config/params.php 中以秒为单位设置超时:

'sessionTimeoutSeconds' => '1800',

In you controllers/SiteController.php actionLogin() method add the following:

在您的控制器/SiteController.php actionLogin() 方法中添加以下内容:

// Set the user session timeout
Yii::$app->session->set('userSessionTimeout', time() + Yii::$app->params['sessionTimeoutSeconds']);

Also add the beforeAction method in the SiteController.php

还要在 SiteController.php 中添加 beforeAction 方法

public function beforeAction($action)
{

    if (!parent::beforeAction($action)) {
        return false;
    }

    // Check only when the user is logged in
    if ( !Yii::$app->user->isGuest)  {
        if (Yii::$app->session['userSessionTimeout'] < time()) {
            Yii::$app->user->logout();
        } else {
            Yii::$app->session->set('userSessionTimeout', time() + Yii::$app->params['sessionTimeoutSeconds']);
            return true; 
        }
    } else {
        return true;
    }
}

In your views/layouts/main.php: Between the head DOM to add the auto refresh header to sent the app back to login view.

在您的 views/layouts/main.php: 头部 DOM 之间添加自动刷新标头以将应用程序发送回登录视图。

<? if (!Yii::$app->user->isGuest) { ?>
            <meta http-equiv="refresh" content="<?php echo Yii::$app->params['sessionTimeoutSeconds'];?>;"/>
<? } ?>