Question

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?

Was it helpful?

Solution 5

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.

OTHER TIPS

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

In cake php you can create session like this

$this->request->session()->write('user_id', 10);

and you can read session value like this

echo $this->request->session()->read('user_id');

Super Simple!

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://book.cakephp.org/2.0/en/core-libraries/components/sessions.html Used in Controllers

http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html Used in Views

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...

when I have strange session behavior, and this help me.

MODEL:

 function clearAllDBCache() {
    $db =& ConnectionManager::getDataSource($this->useDbConfig);
    $db->_queryCache = array();
   }

`

Acess your Helper SessionHelper in lib/Cake/View/Helper/SessionHelper.php and add the method:

public function write($name = null) {
    return CakeSession::write($name);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top