Yii Framework Forum: Loses Session Values - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Loses Session Values Rate Topic: -----

#1 User is offline   darklord 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 01-August 11

  Posted 06 March 2013 - 12:53 AM

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.
Please kindly point me to the right direction.
Thank you in advance.
0

#2 User is offline   Manoj M 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 09-September 12
  • Location:Dharapuram

Posted 06 March 2013 - 04:06 AM

Yii::app()->session['urvariable'];

and call this anywhere
0

#3 User is offline   darklord 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 01-August 11

Posted 06 March 2013 - 09:35 AM

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

Yii::app()->session['key1']; 
// should be equivalent to:
$session=Yii::app()->session;
$key1=$session->get('key1');

0

#4 User is offline   mickey85 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 05-October 11

Posted 06 March 2013 - 10:27 AM

try using Yii::app()->user->setState("key", "value")
11Betting - a free sports betting game
0

#5 User is offline   darklord 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 01-August 11

Posted 06 March 2013 - 10:53 AM

View Postmickey85, on 06 March 2013 - 10:27 AM, said:

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


Thank you for your suggestion.
I will give this a try and keep you posted.
::)
0

#6 User is offline   kokomo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 284
  • Joined: 23-July 10

Posted 06 March 2013 - 11:08 AM

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
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().
0

#7 User is offline   darklord 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 01-August 11

Posted 06 March 2013 - 11:29 AM

View Postkokomo, on 06 March 2013 - 11:08 AM, said:

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
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 for your great suggestion. The autoStart = false setting might definitely cause this issue. I will try it out and keep you posted.
0

#8 User is offline   darklord 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 01-August 11

Posted 07 March 2013 - 01:02 AM

View PostDark Lord, on 06 March 2013 - 10:53 AM, said:

Thank you for your suggestion.
I will give this a try and keep you posted.
::)

This is solution does not work for me.
0

#9 User is offline   darklord 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 01-August 11

Posted 07 March 2013 - 01:03 AM

View PostDark Lord, on 07 March 2013 - 01:02 AM, said:

This is solution does not work for me.

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

#10 User is offline   darklord 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 01-August 11

Posted 07 March 2013 - 07:55 AM

View PostDark Lord, on 07 March 2013 - 01:03 AM, said:

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!
:) :)
0

#11 User is offline   geb 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 12-December 12

Posted 22 March 2013 - 11:52 AM

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);
   }

}

0

#12 User is offline   darklord 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 01-August 11

Posted 22 March 2013 - 01:01 PM

View Postgeb, on 22 March 2013 - 11:52 AM, said:

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!
0

#13 User is offline   donghui 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 05-February 13

Posted 23 October 2013 - 08:27 AM

View Postdarklord, on 22 March 2013 - 01:01 PM, said:

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();
parent::redirect($url,$terminate,$statusCode);

Thanks for your help.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users