Storing State Vs Session Data

What is the difference between the following two functions:

Yii::app()->user->setState(‘key’, value);



Are they functionally equivalent? Is there a time when you should use one over the other?

I’d say they are functionally equivalent, but semantically different.

I’d personally use Yii::app()->user->setState() to only store information about the user’s account. This can be set when they log in and allow you to store extra detail about the user without having to access the database each time.

I’d use the session component for all other session state.

Your avatar looks a bit … Dangeresque B)

They are not equivalent. If you store data with setState(), the keys are prefixed with a unique prefix (see CWebUser::getStateKeyPrefix).

And there’s a difference if you call logout(), which depends on the argument:

  • logout() - Delete everything from session

  • logout(false) - Only delete data from user state and keep all other session data

And one more thing: Don’t confuse CWebUser::setState with CUserIdentity::setState. Data you store with the latter is not only saved in state, but also in a cookie on the clientside, if you enable the allowAutoLogin feature! So be very careful with this.

Thank you, this got me on the right track…

I would like to add the following:

Information set with CUserIdentity::setState (in authenticate, where else?) can be read with CWebUser::getState.

It will still be available after the session ended and the user logged in again via a cookie (if allowAutoLogin=true).

Can someone point me to where I can find more information about this topic? I’m using Lot’s of soap calls of which none is stored in a state except for the login which I think it’s using sessions.

I think I can speed up my application a lot with this technique.