CakePHP 3 losing Auth Session - session

I am using Auth Session in CakePHP.
'Session' => [
'timeout' => 0,
'defaults' => 'php',
'ini' => [
'session.cookie_secure' => false,
'session.cookie_lifetime' => 0
]
],
Already configured this in tha app.php but still getting logged out after few minutes.
Although Session persists when closing Browser. So i think its just a time related problem.

I was having requirement to increase session timeout in cakphp 2.5
the following code worked for me in 2.5 you can give this a try
//FILE: config/core.php
Configure::write('Session', array(
'defaults' => 'cake',
'cookie' => 'my_app',
'timeout' => 500, // 8 hours + 20 min, user will be logged in till 8 hours.
)
);

Related

Cakephp3 Change cache duration in controller

Hello everyone I work with cakephp3 here is my cache configuration in app.php
'Cache' => [
'access_token' => [
'className' => 'File',
'duration' => '+2 minutes',
'path' => CACHE
]
],
I set the duration to 2 minutes but I need to dynamically change the duration via my Controller.
I I tried setConfig but it does not work
Cache::write("access_token_$key", $response, 'access_token');
Cache::setConfig("access_token_$key", array('duration' => $time));
Also i tried this but still does not work
$engine = Cache::engine("access_token_$key");
$engine->config("duration", $time);
Thanks for any help

Yii2 set session timeout - I am unble to set session timeout in yii2

Below is the configuration I used backend/config/main.php in my application But still I am unable to set session timeout.
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'authTimeout' => 3600,
],'session' => [
'class' => 'yii\web\Session',
'cookieParams' => ['httponly' => true, 'lifetime' => 3600 * 4],
'timeout' => 3600*4,
'useCookies' => true,
],
My requirements are
1.Expire session if inactive of user more than 30 minutes
2.If browser is closed, don't set session expire before 30 mins.
Thank in advance

Yii2 session expires after user is idle for a fixed seconds despite session timeouts being set to at least 1 day

I have already added these codes in my config/web file
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
'enableSession' => true,
'authTimeout' =>86400,
'loginUrl' => ['account/login'],
],
'session' => [
'timeout' => 86400,
],
After session expires I want to automatically logout and redirect to login action.
Make sure in your php.ini file
config
session.gc_maxlifetime
by default set as :
session.gc_maxlifetime=1440
change it, to :
session.gc_maxlifetime=86400
Very first you have to set 'enableAutoLogin' => false, .
now add these lines there in your config/web.
I have added it in frontend/config/main.php because I am using frontend only.
'components' => [
...
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => false,
'enableSession' => true,
'authTimeout' => 1800, //30 minutes
'identityCookie' => ['name' => '_identity-frontend', 'httpOnly' => true],
],
'session' => [
// this is the name of the session cookie used for login on the frontend
'class' => 'yii\web\Session',
'name' => 'advanced-frontend',
'timeout' => 1800,
],
...
Now go to yii2/web/User.php and write code for destroy session in logout method before return as guest()-
public function logout($destroySession = true)
{
$identity = $this->getIdentity();
if ($identity !== null && $this->beforeLogout($identity)) {
......
if ($destroySession && $this->enableSession) {
Yii::$app->getSession()->destroy();
}
$this->afterLogout($identity);
}
$session = Yii::$app->session;
$session->remove('other.id');
$session->remove('other.name');
// (or) if is optional if above won't works
unset($_SESSION['class.id']);
unset($_SESSION['class.name']);
// (or) if is optional if above won't works
unset($session['other.id']);
unset($session['other.name']);
return $this->getIsGuest();
}
For me it worked great.

CakePHP continue session outside cake

I'm trying to continue the CakePHP session outside the application.
The CakePHP session config:
Configure::write('Session', array(
'checkAgent' => false,
'defaults' => 'cake',
'timeout' => 10080, // 1 week,
'ini' => array(
'session.cookie_httponly' => 1,
)
));
cakephp_webroot/test_session.php:
<?php
session_name("CAKEPHP");
session_start();
var_dump($_SESSION);
?>
test session.php should output the cake session, but it is not working. I've verified the cookie CAKEPHP is present.
You are using the cake defaults for session handling, they are not compatible with the PHP defaults (available as php for the defaults option).
The cake configuration uses a custom save path and enforces cookie usage.
https://github.com/cakephp/.../Datasource/CakeSession.php#L600-L612
// ...
'cake' => array(
'cookie' => 'CAKEPHP',
'timeout' => 240,
'ini' => array(
'session.use_trans_sid' => 0,
'url_rewriter.tags' => '',
'session.serialize_handler' => 'php',
'session.use_cookies' => 1,
'session.cookie_path' => self::$path,
'session.save_path' => TMP . 'sessions',
'session.save_handler' => 'files'
)
),
// ...
So either configure your external scripts session usage the same as the cake defaults, or use the php defaults instead, and control things via your PHP ini configuration.
See also
Cookbook > Development > Sessions > Built-in Session handlers & configuration
http://php.net/manual/en/session.configuration.php

Cakephp Session expiration doesn't work?

I'm pretty new to Cakephp. I'd like my sessions data gets expired in 3 days. However, it seems like the expiration time is just a few hours, as when the user logs in, s/he will be logged out in a few hours.
Here is all the changes I have made in core.php:
I added timeout parameter:
Configure::write('Session', array(
'defaults' => 'php',
'timeout' => 4320
));
I checked most of the relevant questions and none of the solutions worked for me:
Changing 'timeout' to 'session.timeout', changing 4320 to '4320' and ,...
Thanks for you help in advance. :)
You can use below code for same:
Configure::write('Session', array(
'defaults' => 'php',
'timeout' => 30, // The session will timeout after 30 minutes of inactivity
'cookieTimeout' => 1440, // The session cookie will live for at most 24 hours, this does not effect session timeouts
'checkAgent' => false,
'autoRegenerate' => true, // causes the session expiration time to reset on each page load
));
Read more here

Resources