I am trying to set some session variable in Yii using the following code :
$session=new CHttpSession;
$session->open() ;
$session->setSessionName('My Session') ;
$session['sleep'] = 0 ;
$session['attempts'] = 0 ;
$session->writeSession('sleep','0') ;
$session['ip'] = $this->get_ip_address() ; $session->close() ; var_dump($session,$session['ip']) ;
However , I am not able to set the session vaiables above , the dump has the following result :
object(CHttpSession)#17 (5) { ["autoStart"]=> bool(true) ["behaviors"]=> array(0) { } ["_initialized":"CApplicationComponent":private]=> bool(false) ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } string(3) "::1"
It sees $session and $session['ip'] as two completely different variables . Can someone help me out with this ?
I work with sessions under Yii in another way, I use the global session variable under the app() variable. So you can store values like this:
Yii::app()->session['sleep'] = "value";
And you can get values like this:
$sleep = Yii::app()->session['sleep'];
Finally you can remove it like this:
unset(Yii::app()->session['sleep']);
This way you can access them everywhere in your code. I recommend you to read this article: http://www.larryullman.com/2011/05/03/using-sessions-with-the-yii-framework/ it has all the information that you need.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With