Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii session management

Tags:

php

session

yii

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 ?

like image 261
Varun Jain Avatar asked Nov 24 '12 13:11

Varun Jain


1 Answers

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.

like image 55
Skatox Avatar answered Oct 01 '22 14:10

Skatox