Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read and write Session in Cakephp 3.0

I am new to cake 3.0. I have read documentation on http://book.cakephp.org/3.0/en/development/sessions.html But I am not able to write sessions.

use Cake\Network\Session\DatabaseSession;

$session->write('Config.language', 'eng'); 
$session->read('Config.language');
like image 892
Amuk Saxena Avatar asked Jun 01 '15 08:06

Amuk Saxena


People also ask

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 can I use session in CakePHP 4?

A basic example of session usage in controllers, views and cells would be: $name = $this->request->getSession()->read('User.name'); // If you are accessing the session multiple times, // you will probably want a local variable. $session = $this->request->getSession(); $name = $session->read('User.name');

How to destroy session in CakePHP?

Destroying the Session To destroy a session, use the destroy() method: $session->destroy();

How can I increase my session timeout in CakePHP 3?

If you need to change it, either do that in your php. ini , or use the ini option in the CakePHP session configuration. Quote from the docs: By default PHP sets the session cookie to expire as soon as the browser is closed, regardless of the configured Session.


1 Answers

Update : For CakePHP 3.6+, see @shubham715 answer

The following applies to CakePHP before 3.6 :

You need to set $session :

$session = $this->request->session();
$session->write('Config.language', 'eng'); 
$session->read('Config.language');

And then you'll be able to read and write in your session

Or you can direclty read and write :

$this->request->session()->write('Config.language', 'eng');
$this->request->session()->read('Config.language');
like image 110
Jun Avatar answered Oct 31 '22 15:10

Jun