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

后端 未结 5 1745
南笙
南笙 2020-12-08 20:41

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

return array(
...
    \'components\'=>array(
        \'session\' => array(
                     


        
相关标签:
5条回答
  • 2020-12-08 21:03

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

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

    add those are text 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:

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

    0 讨论(0)
  • 2020-12-08 21:04

    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:

    session.gc_maxlifetime

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

    session.gc_maxlifetime = 86400

    24 hours. Problem Solved for me.

    Hope this could help someone!

    0 讨论(0)
  • 2020-12-08 21:13

    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.

    0 讨论(0)
  • 2020-12-08 21:17

    For Yii2 version

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

    'sessionTimeoutSeconds' => '1800',
    

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

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

    Also add the beforeAction method in the SiteController.php

    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.

    <? if (!Yii::$app->user->isGuest) { ?>
                <meta http-equiv="refresh" content="<?php echo Yii::$app->params['sessionTimeoutSeconds'];?>;"/>
    <? } ?>
    
    0 讨论(0)
  • 2020-12-08 21:20

    For Yii2

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

    'components' => [
        'session' => [
            'class' => 'yii\web\Session',
            'cookieParams' => ['lifetime' => 7 * 24 *60 * 60]
        ],
    
    0 讨论(0)
提交回复
热议问题