session handling

hi…

I am storing a session data in my controller for selecting category…


$categorySelected = $_GET['select_user'];

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

		switch (Yii::app()->session['category'])

		{

		case 'patient':

		$this->render('patient');

		break;

then I am trying to use this session variable to the view pages for automatic display of category information like…


<div class="row">

		

		<?php echo $form->labelEx($model,'category'); ?>

		<?php echo $form->textField($model,'category',array('size'=>20,'value'=> Yii::app()->session['category'], 'readonly'=>'readonly')); ?>

		<?php echo $form->error($model,'category'); ?>

	</div>

but i am not getting any value in the category textfield…

plz help

if anybody have any idea how to solve this problem plz reply…

If you don’t get any value it can mean that $_GET[‘select_user’] is null

You can check this simply by trying to use this line


Yii::app()->session['category'] = 'sometext';

and you will see if that text will be in the textfield…

Anyway… in the view… instead of using htmlOption[‘value’] you can assign that value to the model field before the call to textField… like:


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

thanks mdomba for your reply but


$_GET['select_user']

is not null I am able to access the session data on the same page but when i am accessing it in my view page to display the selected category that time i am not getting any value????

Why don’t you just write the “category” property of the model, something like:




$categorySelected = $_GET['select_user'];

$model=new WhateverModel;

$model->category=$categorySelected ;


switch ($categorySelected)

{

 case 'patient':

 $this->render('patient');

 break;



Then in the view, just:




<div class="row">

                

                <?php echo $form->labelEx($model,'category'); ?>

                <?php echo $form->textField($model,'category',array('size'=>20, 'readonly'=>'readonly')); ?>

                <?php echo $form->error($model,'category'); ?>

        </div>



Yes that would be an option but the actual scenario is little different. I am showing the category in the registration page and according to the category selected in the first page the registration form and rest of the page differs in view. So i think can’t set the category in the model before user registration.

Could be something with the PHP settings about session handling…

Here is a little script to test if session is working… copy/paste it to a file "page1.php"… everytime you click the link the views output should be incremented…




<?php

session_start();

if(isset($_SESSION['views']))

    $_SESSION['views'] = $_SESSION['views']+ 1;

else

    $_SESSION['views'] = 1;


echo "views = ". $_SESSION['views'];

echo '<p><a href="page1.php">Refresh</a></p>';

?>



If this is not working you need to check the session save path that it points to an existing folder…

Thank you for the code

The above is working for me


views = 22

Refresh

I think there might be some other problem

Interesting…

Can you check:

Maybe you should try to use:

  1. Yii::app()->user->setState(‘category’,$category) for category saving

  2. Yii::app()->user->getState(‘category’) for category getting

I really don’t know if it would solve your problem, but you could try :)

it is interesting topic on session management in yii.Where can i find page1.php?can you suggest me some good wiki articles on session management.i tried this code for maintaing my session

in config.php i enabled autostart to true in components array.

in useridentity.php i have kept the following code




$session=new CHttpSession;

                    $session->open();

                    $session['societyId']=$societyusers->societyid;  // set session variable 'name3'

                    $session['societyName']=$societyusers->societyname;

                    $this->setState('societyName', $societyusers->societyname);

                    $session->close();



and in loginform.php i kept code as




Yii::app()->societyusers->getState(societyname);



but when i login its not showing anything do i have to do something more than this.how can i know whether a session is working or not?please guide me

thanks

ramakrishna

This is not a Yii file… you just create a new empty file “page1.php” and put in it the code above… than from the browser you run that file… every time you click the link… the counter should increase… if it’s not increasing you have to check the session configuration in PHP…

As for the articles don’t know what to suggest… just go to the wiki page and search for session…

this code is working when i create a file called page1.php and run it from browser.so there is no problem with my session configuration in php.problem is only with my code i think?do i have to enable or set any rules in controller?

Thanks Maurizio, this was a great help! after searching for a long while, you gave me the final input… everything works fine now!