Yii2 timeout settings

Hello,

I want to put the timeout as a config parameter of the application but it does not work. When the value is fixed in web config in user component, it works correctly. Below the config:

‘user’ => [

        'class' => 'amnah\yii2\user\components\User',


    	'enableAutoLogin'=>false,


    	'authTimeout' =>  400,


    ],

To implement it as config parameter, in actionLogin(), i put the below code but it does not work.

            $paramConfig=Tabparamconfig::find()->where("1")->one();


		$sessionTimeout=$paramConfig->timeout;


		yii::$app->user->authTimeout=$sessionTimeout;

Please what is wrong with this code?

BR

"yii::$app" is created anew for every request. It has a very short lifetime. Modifying authTimeout in a login action takes effect only in the current request, not in the subsequent requests.

Thanks for the reply.

What can i do to solve my problem?

BR

I’m sorry, but I might have been wrong with yii\web\User\authTimeout.

I have to check it myself.

I am having the same issue. How do I make a login session last longer then 15-20 minutes. It keeps logging my users out?

It’s not the same issue.

While @rostwich wants to set the authTimeout dynamically in the runtime, you can just set the longer value for it in the application config statically.

yii\web\User::authTimeout

See also

yii\web\Session::timeout

If User::authTimeout is not set, (‘null’ is set as the default value for it), Session::timeout will determine the login session length. It defaults to 1440 seconds (24 minutes).

You can attach an event handler to EVENT_BEFORE_REQUEST event of the application.

http://www.yiiframework.com/doc-2.0/guide-structure-applications.html#application-events

For example, you can write the following in your application configuration:




    'on beforeRequest' => function ($event) {

        $timeout = ...

        Yii::$app->user->authTimeout = $timeout;

    },



in config/web.php I have:




'components' => [

...

        'user' => [

            'identityClass' => 'app\models\User',

            'enableAutoLogin' => false,

            'authTimeout' => 657567576,

            'enableSession' => true,

            'autoRenewCookie' => true,

        ],

        'session' => [

            'class' => 'yii\web\Session',

            'timeout' => 657567576,

        ],

...



The session still times out for me after a short period of time.

Any other ideas?

Sorry, but I don’t have any idea.

You may want to give some information regarding your environment like server, hosting, PHP version, … etc.

BTW, "autoRenewCookie" is not effective when "enableAutoLogin" is false, although it must not be relevant to your current issue.

http://www.yiiframework.com/doc-2.0/yii-web-user.html#$autoRenewCookie-detail

I read somewhere else that "enableAutoLogin" can cause session time out issues, so I tried to turn it off.

BTW, I’ve just posted a relevant topic in “Tips” section.

Set Session Timeout Per User

Some of you may find it useful. :)

Hi,

Thks for your feedback.

I tried your code like this:

‘on beforeRequest’ => function ($event) {

	if(!Yii::$app->user->isGuest){


		 $timeout=Yii::$app->user->identity->sessiontimeout;


		 Yii::$app->user->authTimeout= $timeout;


	}		


},

But i have some problems:

When the value is fixed like this it works fine:

‘on beforeRequest’ => function ($event) {

                     $timeout=15;


		 Yii::$app->user->authTimeout= $timeout;		


},

but when the code is as my first code, it does not work.

I do not understand what’s the problem.

BR

Switching to DBSessions made it work for me. I am guessing it is a issue on my server with local sessions.

How can i do it (switching to DBSessions)

BR

in config/web.php




        'session' => [

            'class' => 'yii\web\DbSession',

            'timeout' => 60*60*24*14, // 2 weeks

            'sessionTable' => 'YiiSession',

        ],



Create database table.




CREATE TABLE YiiSession

(

    id CHAR(40) NOT NULL PRIMARY KEY,

    expire INTEGER,

    data BLOB

)