How do I create, write, and read session data in CakePHP?
Asked Answered
E

8

13

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?

Extracanonical answered 7/7, 2009 at 8:31 Comment(0)
E
0

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.

Extracanonical answered 20/7, 2009 at 6:38 Comment(0)
C
12

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
Collation answered 7/7, 2009 at 9:53 Comment(1)
short and sweet code and working for all versions :)Clubby
U
7

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!

Underground answered 21/7, 2018 at 15:24 Comment(0)
S
2

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

Selfcontrol answered 7/7, 2009 at 10:13 Comment(0)
S
2

cakephp 4 example of session usage in controllers, views and cells

$session = $this->request->getSession();
$session->write('abc', 'apple');
echo $session->read('abc');
Symbolize answered 25/6, 2020 at 3:54 Comment(0)
H
0

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

Hiroshima answered 7/7, 2009 at 10:42 Comment(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.Extracanonical
Like I mentioned, you can use the SessionHelper in your views. See book.cakephp.org/view/484/Session and book.cakephp.org/view/567/MethodsHiroshima
E
0

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.

Extracanonical answered 20/7, 2009 at 6:38 Comment(0)
P
0

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

MODEL:

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

`

Polytheism answered 2/2, 2010 at 19:36 Comment(0)
E
0

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

public function write($name = null) {
    return CakeSession::write($name);
}
Electrotechnics answered 18/4, 2014 at 13:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.