views:

1739

answers:

6

Hi, can anyone give me an example on how to create Sessions and write data to it. I've seen syntax on how to write data to a session using write command. But how to create a session and retrieve the values in it.

In my application, I have two data, form_id and user_id that needs to be used in all the page requests. So how do I save it as a session variable and use it across the application?

EDIT

function register()
{
 $userId=$this->User->registerUser($this->data);
 $this->Session->write('User.UserId',$userId);
 //echo "session".$this->Session->read('User.UserId');
 $this->User->data=$this->data;
    if (!$this->User->validates())
    {
    $this->Flash('Please enter valid inputs','/forms' );
      return; 
    }

$this->Flash('User account created','/forms/homepage/'.$userId);            

}

How to use the session variable 'User.UserId' instead of $userId in $this->Flash('User account created','/forms/homepage/'.$userId);

And can I use this variable in all my view files,because in all the page requests I also pass the userId?

EDIT 2

I have 2 controllers,user and form. I write the userid to a session variable in the users*_controller. I have a view file called homepage.ctp,whose action is in the forms_controller.* Now how can I use the session variable defined in the users_controller in the homepage? Sorry if I am asking silly questions. I went through the cakebook,but my doubts weren't cleared. I'm also trying trial and error method of coding,so please help me.

EDIT 3

I have a session variable 'uid' which is the user id in the home page action of a controller.

       $this->Session->write('uid',$this->data['Form']['created_by']);

I need the same variable in the design action method of the same controller. When I give

             $uid=$this->Session->read('uid');
            echo "uid: ".$uid;

the value is not echoed.

Can't I use the session variable in the same controller?

A: 

The bakery is your best friend:

http://book.cakephp.org/view/398/Methods

All your session read/writes belong in the controller:

$this->Session->write('Person.eyeColor', 'Green');

echo $this->Session->read('Person.eyeColor'); // Green
Mike B
A: 

You don't have to write any code to create session, they are already built in. Then you just use the read and write sessions as mentioned above. Also see here for more details:

http://api.cakephp.org/class/session-component Used in Controllers

http://api.cakephp.org/class/session-helper Used in Views

jimiyash
A: 

In this case it would be:

$this->Flash('User account created','/forms/homepage/'.$this->Session->read('User.UserId'));

and your second question is anwered by Jason Miy (http://api.cakephp.org/class/session-helper). You can simply use this in your view:

$userId = $session->read('User.UserId');

Reading the appropriate cookbook pages slowly and carefully usually helps a lot...

dr Hannibal Lecter
I have 2 controllers,user and form. I write the userid to a session variable in the users_controller. I have a view file called homepage.ctp,whose action is in the forms_controller. Now how can I use the session variable defined in the users_controller in the homepage? Sorry if I am asking silly questions. I went through the cakebook,but my doubts weren't cleared. I'm also trying trial and error method of coding,so please help me.
Angeline Aarthi
Like I mentioned, you can use the SessionHelper in your views. See http://book.cakephp.org/view/484/Session and http://book.cakephp.org/view/567/Methods
dr Hannibal Lecter
A: 

I found out the reason why the uid wasn't being echoed(edit 3 part of the question). It is due to a silly mistake, had a white space after the end tag ?> in the controller. Now it is working fine.

Angeline Aarthi
A: 

How to set the session variable in view. In tutorial it is said that we should not use the write method in view. Then what is the other method to save the variable in the session.

Sateesh
A: 

when i have strange session behawior, and this help me,` MODEL: function clearAllDBCache() { $db =& ConnectionManager::getDataSource($this->useDbConfig); $db->_queryCache = array(); }

`

Andrew

related questions