Loses Session Values

I am using CDbHttpSession to handle session variables in my exciting Yii application. However,

I’ve run into an issue where my session variables got cleared out after redirecting to another page or just refreshing the page using $this->redirect() or $this->referesh() respectively. I’ve been dealing with this issue for the last couple days. Therefore, any help would be very appreciated.

Here is my setup:

// protected/main.php

// I created my yii_sessions table and saw the data got populated in the session table 

'session' => array (

			'sessionName' => 'CouponSession',

			'class'=> 'CDbHttpSession',

			'autoCreateSessionTable'=> false,

			'connectionID' => 'db',

			'sessionTableName' => 'yii_sessions',

			'autoStart' => 'false',

			'cookieMode' => 'only',

			'timeout' => 1800, // 1800/60 = 30 minutes; timeout is in seconds


// here is my table definition in the postgresql database

CREATE TABLE yii_sessions


  id character(32) NOT NULL,

  expire integer,

  data bytea,

  CONSTRAINT yii_sessions_pkey PRIMARY KEY (id)


// In controller, I am doing something like

$session = Yii::app()->session;




// in the protected/views/home/index.php

// I am trying to retrieve my session variable values



  $key1 =$session->get('key1');

  $key2 =$session->get('key2'); 

  // here I lost all my session variable values

  // I have confirmed the session persists values fine if I am on the same view and controller

  // However, after redirection, all session values disappears


I’ve been searching in forums but I didn’t find any suggested fix for this issue.

Please kindly point me to the right direction.

Thank you in advance.


and call this anywhere

Thank for your suggestion. I have tried that as well. However, the problem persists.

According to the documentation,


// should be equivalent to:



try using Yii::app()->user->setState("key", "value")

Thank you for your suggestion.

I will give this a try and keep you posted.


You set


'autostart' => false,


in your session component config array so I think maybe this causes your behaviour in resetting session after redirect.

Try setting and getting a session value the standard php way like

$_SESSION['key1'] = 'value1';


// after redirect


Also set YII_DEBUG to true (if not already done) and log all levels (at least warn) in one file, etc. via a log route and search for "Failed to start session." which is logged in CHttpSession.open().

Thank you for your great suggestion. The autoStart = false setting might definitely cause this issue. I will try it out and keep you posted.

This is solution does not work for me.

Somehow, changing the autoStart = true does not resolve my issue.

Thank you all for your guidance and wisdom. With your helps, I finally resolved my stubborn session issue. Here what I found out:

I should always assign values to session variables in controllers ONLY. Assigning values to session variables, say, in component classes whould not work in multiple pages. All session variables that were set in component classes got cleared out after redirection or page refresh.

Now, I can continue my projects.

Happy coding!

:) :)

That’s the way I dealt with

class Controller extends CController 


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


      if(session_id()!=='') @session_write_close();

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



Thanks for sharing! My CDbHttpSession is working great now!

Hey, i am wondering if your issus has been resolved? by using Controller public function redirect($url, $terminate=true, $statusCode=302) … if(session_id()!==’’) @session_write_close();


Thanks for your help.