Yii 1.1x logs out the user before session timeout - php

I'm using Yii 1.1.13 and despite the session timeout is set to 1 day (session.gc_maxlifetime = 86400 in php.ini) Yii logs out the user after ~24 mins. It doesn't even redirect to the login page, only when the user would navigate to another page.
I set these in the login function:
$duration = Yii::app()->getSession()->getTimeout();
Yii::app()->user->login($this->_identity,$duration);
(the Yii::app()->getSession()->getTimeout() call gives back the right time, that is 86400)
I also set the cookie lifetime to this value:
Yii::app()->request->cookies['PHPSESSID']->expire = Yii::app()->getSession()->getTimeout();
but the bug still appears.
Thank you in advance for your helping answers.

I needed to set this in my main.php:
'session' => array(
'timeout' => 86400,
'cookieParams' => array(
'lifetime' => 86400,
),
)
It keeps alive both the session and the cookie even if they are somehow given a lower value.

Related

How do I set session timeout in CakePHP

I am using CakePHP 2.x and I want to know how do I set session time out to 8hrs. Please note that session should not expire before 8hrs. I have used
Configure::write('Session.timeout', '480');
but session still expires after few minutes.
Can anyone tell how do I set session time out to 8hrs, so that it will expire exactly after 8hrs?
Reset the session cookie expiration date:
$this->Session->renew().
UPD: you can also do that by:
Configure::write('Session', array(
'timeout' => '480',
'autoRegenerate' => true
)
);

Cakephp Cookie gets deleted automatically when session timesout

I am writing a cookie for auto login users.
It works almost flaw less. But when the Session times out the cookie gets deleted, although it's set for 30 days.
I can't understand why this is happening.
If I close the browser and reopen it, all are fine, but if I leave the browser open and let the Session time out the cookie gets deleted to.
Configure::write('Session', array(
'defaults' => 'php',
'cookie' => 'KPD',
'timeout' => 180,
'cookieTimeout' => 30 * 1440
));
UPDATE: I found the problem but I don't have a solution! The problem is that when I rewrite the Cookie nothing happens, even if I try to delete it, and rewrite it.
I have a cookie as an array User.remember = array('token' => TOKEN). When I try to rewrite the token, the cookie remains the same!
Maybe you are not defining the value (in number of minutes) of Session.cookieTimeout, you should define proper value for Session.cookieTimeout. If it is not defined it will use the same value as Session.timeout

How to increase the session life time in cakephp?

I am trying to increase the session lifetime in cakephp app. I have a remember me checkbox in login page. When checking the checkbox, I need to extend session time to 1 hour more to current time and any action inside the app after login will need to extend session time to 1 hour more.
I have component file for login and all action will be entered in startup function.
I tried to extend the session.cookie(CakePHP's session cookie) lifetime, but it didn't works.
function startup(&$controller) {
/* First try */
setcookie(Configure::read('Session.cookie'),$_COOKIE[Configure::read('Session.cookie')], time() + 3600, "/"); // Configure::read('Session.cookie') is 'CAKEPHP'.
/* Second try */
ini_set('session.gc_maxlifetime', 3600);
/* Third try */
session_set_cookie_params(time() + 3600);
echo $this->Session->read('status').' session <br/>';
echo $_SESSION['test'];
}
But all of these doesn't keep the session after session.timeout(CakePHP's session timeout) time. The session status and test varaibles are created on login. It will be retrieved until session.timeout is not reached.
I am using cakephp v1.2.
keep this in your core.php file
Configure::write('Session', array(
'defaults' => 'cake',
'timeout' => 14400, // 4 hours
'cookieTimeout' => 14400, // 4 hours
'cookie' => 'Your Cookie Name',
)
);
It is not a good idea to keep very high session timeout. If your requirement is only to keep him logged for more time, then use some auto_login component like www.milesj.me/resources/script/auto-login

Cookie automatically destroyed when browser is closed

I am using codeigniter and in my library file I am storing the cookie that works fine whenever browser is opened, but expires when I close the browser is any thing is wrong in code?
$this->CI =& get_instance();
$this->CI->load->helper('cookie');
$expire = time()+(60*60*24*30);
$cookie = array(
'name' => 'username',
'value' => $users['username'],
'expire' => $expire
);
$this->CI->input->set_cookie($cookie);
$cookie = array(
'name' => 'password',
'value' => $users['password'],
'expire' => $expire
);
$this->CI->input->set_cookie($cookie);
The problem is the expiration time you are sending ... this is from the help page here
The expiration is set in seconds, which will be added to the current time. Do not include the time, but rather only the number of seconds from now that you wish the cookie to be valid. If the expiration is set to zero the cookie will only last as long as the browser is open.
So change your code to this :
$expire = (60*60*24*30);
To set the cookie to expire 30 days from now
(Although setting it like you did should probably have worked - but the expiration would be years in advance ... the help document also shows the expiration given as a string - maybe thats the problem)

session expires and so does my cookie

I have set cookie and set it to expire after sufficient seconds. Still as soon as my session expires the cookie also expires. This is my code :-
if(isset($_POST['KeepMesignedIn'])) {
$this->load->helper('cookie');
$cookie = array(
'name' => 'info',
'value' => $user->Username . '||' . $user->Password,
'expire' => time()+3600*24*30
);
set_cookie($cookie);
}
Can anybody identify the problem?
According to the CodeIgniter documentation, set_cookie expects expires to be the delta seconds that are added to the current time:
The expiration is set in seconds, which will be added to the current time. Do not include the time, but rather only the number of seconds from now that you wish the cookie to be valid. If the expiration is set to zero the cookie will only last as long as the browser is open.
check is this part of code executed in your app or not. You need to debug.

Categories