I have time in seconds when the session should end. If the user has not selected the checkbox "remember_me" - the session will last 2 hours. When the checkbox is selected - should last - 48 hours. I have a loginСontroller, where I react - to the login result and if the validation is successful and checkbox = "on" you need to change the session time. I tried to change by looking at the documentation, and spent a lot of time looking for a solution. Maybe someone can help me. Thank you very much in advance[enter image description here]
here is my file config/app.php
'Session' => [
'defaults' => 'php',
'ini' => [
'session.cookie_lifetime' => 7200,
]
],
and here is my loginController
`public function index()
{
$this->viewBuilder()->setLayout('main');
$this->set("title", "");
$this->set("description", "description");
$this->request->allowMethod(['get', 'post']);
$result = $this->Authentication->getResult();
// regardless of POST or GET, redirect if user is logged in
if ($result->isValid()) {
if ($this->request->getData('remember') == 'on') {
///// the solution should be here
}
$redirect = [
'controller' => 'Main',
'action' => 'index',
];
return $this->redirect($redirect);
}
// display error if user submitted and authentication failed
if ($this->request->is('post') && !$result->isValid()) {
$this->Flash->saved_error(__('Invalid email or password'));
}
}`
My Answer:
To change the session cookie lifetime in CakePHP 4, you can modify the session configuration in your `config/app.php` file.
1. Open your `config/app.php` file.
2. Locate the `Session` configuration section. It should look something like this:
php
'Session' => [
'defaults' => 'php',
'timeout' => 1440,
'cookie_lifetime' => 0,
],
3. Change the `cookie_lifetime` value to the desired time in seconds when the session should end. For example, if you want the session to end after 1 hour (3600 seconds), you can set it like this:
php
'cookie_lifetime' => 3600,
4. Save the `config/app.php` file.
Now, the session cookie lifetime should be set to the specified time in seconds. Remember that the session cookie lifetime is different from the session timeout, which determines how long the session data will be stored on the server.
Rate this post
4 of 5 based on 9499 votesComments