Get User States

hello everyone,

i am trying to fetch the current user’s all states.

since all the user related data is stored in CWebUser object so i used this statement Yii::app()->user.

It returned me the full object but cannot find the states.

In the documentation(please refer : http://www.yiiframework.com/doc/api/1.1/CWebUser) there is function getState() which requires the name of the state but i want to fetch all the user states irrespective of providing the parameter to fetch the state.

Please help!!

Thanks,

Mohit

as you can see in CWebUser source there is no single function to get all states, but:




public function getState($key,$defaultValue=null)

	{

		$key=$this->getStateKeyPrefix().$key;

		return isset($_SESSION[$key]) ? $_SESSION[$key] : $defaultValue;

	}



so you can iterate session array and check for keys that start with ‘stateKeyPrefix’, something like:




$prefix = Yii::app()->user->stateKeyPrefix;

$states = array();

foreach( $_SESSION as $k=>$v ) {

  if( strpos($k, $prefix) === 0 ) {

    states[ substr( $k, strlen( $prefix ) ) ] = $v;

  }

}