User->Setstate Vs Session

Hi All

I want to reserve data among the user requests (like a shopping card does)

What is the difference between


Yii::app()->session['data1']='value1'  and  Yii::app()->user->setState('data1', 'value1');

what are the prons and cons and why/when use the one instead the other?

Thanks in advance

No one knows? :unsure:

Just look at the source code of those methods, one sets


$_SESSION['data1']

and the other sets


$_SESSION[md5('Yii.' . get_class($this) . '.' . Yii::app()->getId()) . 'data1']

So setState() will rarely conflict with other applications.

Hi @Tsunami and thanks for your response!

But I thing there is a more specific reason that Yii developers include both of two ways over the conflict issue.

So does anyone knows any other reason?

Thanks!

Can’t tell you their initial reason to provide CWebUser::setState(), but the method will not make it into yii 2. See issue #167 on github. It’s really only a tiny helper that handles key prefixing, and it seemingly causes too much confusion for what it’s worth.

Thanks Ben!

QiangXue, Samdark and Ragazzo gave enough information about that

The link was very useful! :)