문제

I am creating an web application in Yii . I was trying to do a sessiontimeout, if a user is idle for 30 minutes.After that he should login again.. but this is not working. I am using CHttpSession. However if i give CDbHttpSession instead of CHttpSession this is working fine.

this is my code

'user' => array(
        'class' => 'WebUser',
        'loginUrl' => array('site/loginaccount'),
        'allowAutoLogin' => true,
    ),
            // uncomment the following to enable URLs in path-format
            'session' => array(
       'class'=>'CHttpSession',
        'timeout'=>$params['session_timeout'],
        'autoStart'=>true,
    ),

Is there anything else to make this work for CHttpSession ? Due to some reasons i cannot use CDbHttpSession in my web application .

도움이 되었습니까?

해결책

If you want that the user is sign out automatically after 30 minutes try:

'user' => array(
    'class' => 'WebUser',
    'loginUrl' => array('site/loginaccount'),
    'allowAutoLogin' => true,
    'authTimeout' => 1800
),

다른 팁

protected/config/main.php : (define the session timeout)

$sessionTimeout = 5; // 5 secondes

return array(
        'params'=>array(
          'session_timeout'=> $sessionTimeout,
        );
        'components'=>array(
                'session' => array(
                        'class' => 'CDbHttpSession',
                        'timeout' => $sessionTimeout,
                ),
        ),
);

protected/views/layout/main.php : (define the refresh)

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

I've read the source code of the CHttpSession. It is a wrap of the PHP Session. So, the mechanism of CHttpSession is the same with the PHP Session.

public function setTimeout($value)
{
    ini_set('session.gc_maxlifetime',$value);
}

the above is the code of timeout setter. it is just the setting of ini settings of the PHP. And according to the PHP documentation of session, after the maxlifetime, the session is just "potentially cleaned up", not for sure.

And the probability of it can be set by session.gc_probability. the default value is 1, which means 1%. So, you can set it to 100, make the garbage collection process run every time the script is run.

change your setting to

'session' => array(
    'class'=>'CHttpSession',
    'timeout'=>$params['session_timeout'],
    'autoStart'=>true,
    'gCProbability' => 100,
),

hope it helps.

return array('components'=>array(
      'session'=>array( 
            'timeout' => 1800
        ),
    ),
);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top