Quick Question: Dynamically Set Chttpsession's Timout

I’m using in main.php:




                'session' => array(

                    'class' => 'CDbHttpSession',

                    'timeout' => 180,

                ),




But I want to set the timeout programatically. In my UserIdentity class after successful login I use:




Yii::app()->getSession()->setTimeout(30); 




But this seems to do nothing. How do I set the timeout?

Not sure but I think the CDbHttpSession class doesn’t support this as it doesn’t override the setTimeout() method (to make a db call). This lead me to try deferring the session open, creating the CDbHttpSession object myself, setting the timeout, then opening (starting) the session (I assume the open would use the configured/set timeout).

This didn’t work either. It seems the session was already started. This is true even though I set autoStart to false. Are there circumstances where Yii starts the session for you even when autoStart = false?

Anybody know what is going on here?

I would assume that when you call getSession(), you’re forcing Yii to create the session if it doesn’t already exist so that it can return the session object to you.

EDIT:

Looking at the code, this seems to be incorrect.

What logic are you using to determine the timeout length?

EDIT 2:

What’s the output if you write




    Yii::app()->session->setTimeout(30);

    echo Yii::app()->session->getTimeout();



I feel like your code should work as long as the session hasn’t been started and the ini values are being correctly updated.

The answer is 30. But I think the issue is not that the object’s setting is not being changed. Rather, I think the setting is not used post session start. The values must be set prior to the session being opened. At least that is my belief. The documentation for CHttpSession even says “Note, these properties must be set before the session is started.” where properties includes settings like timeout.

That is why I was trying to defer the session start, so I could make the setting before the open. Unfortunately I think a session is created and started early on regardless of autoStart (I guess for the the guest user) and thus the opportunity to change this is lost.