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');
In PHP, To get the session id, we have to use like this: session_id();
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');
Destroying the Session To destroy a session, use the destroy() method: $session->destroy();
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.
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With