Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 406
Angeline Avatar asked Jul 07 '09 08:07

Angeline


People also ask

How do you create a session data?

Before you can store any information in session variables, you must first start up the session. To begin a new session, simply call the PHP session_start() function. It will create a new session and generate a unique session ID for the user. The PHP code in the example below simply starts a new session.

How can I use session in cakephp 4?

To use defaults, simply set the 'defaults' key to the name of the default you want to use. You can then override any sub setting by declaring it in your Session config: Configure::write('Session', [ 'defaults' => 'php' ]); The above will use the built-in 'php' session configuration.

How can I get session ID in cakephp 3?

In PHP, To get the session id, we have to use like this: session_id();

How to destroy session in CakePHP?

To destroy a session, use the destroy() method: $session->destroy(); Destroying a session will remove all serverside data in the session, but will not remove the session cookie.


2 Answers

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

$session = $this->request->getSession();
$session->write('abc', 'apple');
echo $session->read('abc');
like image 192
Alimon Karim Avatar answered Oct 04 '22 03:10

Alimon Karim


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
like image 44
Mike B Avatar answered Oct 04 '22 05:10

Mike B