Sari Rahal
Sari Rahal

Reputation: 1955

yii and session variables

Currently working within Yii framework.
I am trying to set a temp array of 16 numbers and refresh the page. However; I ran into a small issue and was trying to find an alternative way of solving this. Currently I was trying to save the array as an attribute in the model, but now I know that can not be done. What I need to do is set a temp variable, check for that temp variable, handle the page accordingly, and then delete the temp variable. I have been reading about Session variables, and it seems like that would be my first choice.

My Questions:

1) Is Session the way to go/Is there a better way?

2) Is Yii using a session, and if so, will I break that?

if(1 && 2){ 3) What is the proper way to instantiate an array in a session and delete it?; }

Upvotes: 1

Views: 919

Answers (2)

Oladapo Omonayajo
Oladapo Omonayajo

Reputation: 980

You can use the setState(), hasState() and getState() methods of the CWebUser class.

//to set the random values
Yii::app()->user->setState('random_key', $theValues);

//to check if the key exists
if (Yii::app()->user->hasState('random_key')) { echo true; }

//to get the random values back
$my_values = Yii::app()->user->getState('random_key');

Here's the link the the CWebUser class reference

Upvotes: 2

chris---
chris---

Reputation: 1546

You can use the $_SESSION global, but its recommended to use Yii::app()->user->setState() or getState(). You have to enable yii sessions in your config in order to use it.

Take a look in the guide.

Upvotes: 1

Related Questions