CDbHttpSession and CController redirect()


I have a problem with CDbHttpSession, it not always writes data to the database when I call redirect()

If anyone knows this problem? This is strange behavior.

I have to directly call


before the




to the session data are stored.


Yii::app()->onEndRequest = array(Yii::app()->session, 'close');

does not help…

I don’t understand why.

When I use CHttpSession session data is saved correctly.

Where are you setting

Yii::app()->onEndRequest = array(Yii::app()->session, 'close');


Make sure it is done after configuring Yii::app()->session and before the call to redirect().

You could put some debug output in CApplication::onEndRequest() to see if it is called.

Also check if your handler is listed in $this->getEventHandlers(‘onEndRequest’);

I added it to the Controller::init() and it was listed in Yii::app()->getEventHandlers(‘onEndRequest’)

public function init() 



  Yii::app()->onEndRequest = array(Yii::app()->session, 'close');


But it didn’t work.

So I overwrote method Controller::redirect()

 public function redirect($url, $terminate=true, $statusCode=302)


  if (Yii::app()->session->useCustomStorage) {



  parent::redirect($url, $terminate, $statusCode);


and it’s fine.

Look at

Sorry for my english.

If it is the only event handler in Yii::app()->getEventHandlers(‘onEndRequest’) I don’t see any reason, why it should not run…

btw: there is no problem with your english :wink:


strange thing… have exactly the same problem here: it looks like, new user session information/variables are lost after using redirect … if they are stored in the same action before redirecting.

I’m searching my source code since hours without success and now I find this post which absolutely fits my problem.

I’m writing data to an user session variables using


Followed by an redirect


In round about 1 of 5 cases the new stored session informations are lost.

This problem mostly exists by using CCacheHttpSession and CDbHttpSession.

Is it possible/planned to change the Yii-redirect-code, (for example) like tybetx wrote… in order to close the session before executing the redirect?!

Thank you, rall0r.


While investigating this problem and playing around with Yii::app()->session->close(); I found out, that not only redirect is the problem.

In my case also the following happend:

1.) the user submits an Form with ajaxSubmitButton

2.) the ajax action runs Yii::app()->user->setState() to set an session variable

3.) the ajax action ends with Yii::app()->end();

4.) the ‘success’ event from the ajaxSubmitButton starts and loads data from the server…

…and does not find the content from "2.)"

I have the same issue!

I setState & than redirect. the problem is the session is LOST!

its hard to reproduce, so i made a fix to the redirect function with session close and deploy to production. and i will see if the problem still exists.

thanks for the help guys, i will try to update

// EDIT ( 19/08/2014 )

this issue was not solved by session->close();

i am losing the session itself, not only the data inside it. it might be the cookie or something of that sort.