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;
$session->add('key1','value1');
$session->add('key2','value2');
$this->redirect($this->createUrl('/home/'));
// in the protected/views/home/index.php
// I am trying to retrieve my session variable values
<?php
$session=Yii::app()->session;
$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.
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
CVarDumper::dump($_SESSION);
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 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.
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();