Yii Session Handling

Hello:

I’m hoping someone can point me in the right direction. What I’m doing is setting a session variable upon login during the actionLogin like so:




 public function actionLogin()

	{

		$model=new LoginForm;


		// if it is ajax validation request

		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')

		{

			echo CActiveForm::validate($model);

			Yii::app()->end();

		}


		// collect user input data

		if(isset($_POST['LoginForm']))

		{

			$model->attributes=$_POST['LoginForm'];

			// validate user input and redirect to the previous page if valid

			if($model->validate() && $model->login()){

				Yii::app()->session['userRole'] = Yii::app()->user->type;

				$this->redirect(Yii::app()->user->returnUrl);

			}

		}

		// display the login form

		$this->render('login',array('model'=>$model));

	}



So in one of my layouts I have the following code:




<?php	


	if(!isset($userRole)){

		$userRole = Yii::app()->session['userRole'];

		

		if(strlen($userRole)==0){

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

			$userRole = $user->type;

		}

	}

	

	$baseURL = Yii::app()->baseUrl;								

?>



The first time that layout is used I’m able to use the $userRole session information. But the NEXT and any SUBSEQUENT times the session data is gone. Anyone have any idea where I’m going wrong?

Thanks.

Does set/getState act in the same way?




// Controller

Yii::app()->user->setState('userRole', Yii::app()->user->type);


//View

echo Yii::app()->user->getState('userRole', 'guest');



you can use,




Yii::app()->user->setState('userRole',Yii::app()->user->type);



in layout,




Yii::app()->user->getState('userRole')



this forum discussion help you to find your answer:

http://www.yiiframework.com/forum/index.php/topic/27232-difference-among-these-approches/