session.gc_maxlifetime not working for me - php

i want to set session time out limit by 3 min ,
i have used this in the page
ini_set("session.gc_maxlifetime", "50"); not working
Solution for this
if (isset($_SESSION['LAST_ACTIVITY'])
&& (time() -
$_SESSION['LAST_ACTIVITY'] > 1800)) {
// last request was more than 30 minates ago
session_destroy(); // destroy session data in storage
session_unset(); // unset $_SESSION variable for the runtime }
$_SESSION['LAST_ACTIVITY'] = time();
// update last activity time stamp

Three variables are used to define the garbage collection behavior of PHP session variables:
session.gc_maxlifetime is the lifetime in seconds for the session
files (default value: 1440 = 24 minutes)
session.gc_probability is
the nominator for the probability to execute the garbage collector
(default = 1)
session.gc_divisor is the denominator for the
probability to execute the garbage collector (default = 100 or 1000)
The nominator and denominator are used together to determine the probability (nominator / denominator). So when session.gc_probability is 1 and session.gc_divisor 100 this is 1 / 100 = 1 %. So 1 % of every page visit (= every session_start call) the garbage collector is executed.
If you want to test how your session expires, you need to set session.gc_probability and session.gc_divisor to 1, so each page visit will cause the garbage collector to run. Furthermore you need to use two different browsers for the test.
The session of the first browser becomes cleaned when you visit your page with the second browser (and the session of the first browser is timed out). In my tests, when you use only one browser, the session becomes automatically extended although it is outdated.

The session will live as long as the file is left on the server's file system. They are cleaned out by a garbage collector. The garbage collector is run approximately every hundred page loads on the server (this is rather random, the "every hundred" page loads is just an average).
Also, the age of the session is inactive age, not total age. The timer will be reset for that session every time the user does a request.

The unit for the session.gc_maxlifetime value is seconds. So you would need to set it to 180 seconds to express 3 minutes.
But besides that, session.gc_maxlifetime is not reliable (see this post for an explanation). You should better implement that on your own to have your session expired after exactly 3 minutes.

Related

How often does a PHP session ID change per user?

Just a basic question, if you open a session when a user visits the main page and you store the session id. When would that user return say another day/time and the id be different?
this depends on how the PHP is configured. specifically these settings control how often a php session id is "erased" by garbage collector:
http://php.net/manual/en/session.configuration.php#ini.session.gc-maxlifetime
session.gc_maxlifetime specifies the number of seconds after which
data will be seen as 'garbage' and potentially cleaned up. Garbage
collection may occur during session start (depending on
session.gc_probability and session.gc_divisor).
http://php.net/manual/en/session.configuration.php#ini.session.gc-divisor
session.gc_divisor coupled with session.gc_probability defines the
probability that the gc (garbage collection) process is started on
every session initialization. The probability is calculated by using
gc_probability/gc_divisor, e.g. 1/100 means there is a 1% chance that
the GC process starts on each request. session.gc_divisor defaults to
100.
http://php.net/manual/en/session.configuration.php#ini.session.gc-probability
session.gc_probability in conjunction with session.gc_divisor is used
to manage probability that the gc (garbage collection) routine is
started. Defaults to 1. See session.gc_divisor for details.
As far as i know the default php session.gc_maxlifetime is 1440 seconds (24 minutes). The more visits you have in your site the most "accurate" these statistics are since all this algorithm will run more often.
A tricky edge case: if you start a session and then NEVER get any other visit to your site, the garbage collector algorithm will never run, hence the session will never expire! If you can understand this, i think you have understood this answer.

CakePHP - Session Timeout - Idle User

I am using Cakephp 1.3 and having a lot of troubles with session timeouts.
So, here's my core.php file.
Configure::write('Session.save', 'cake');
Configure::write('Session.cookie', 'TESTING');
Configure::write('Session.timeout', '0.01');
Configure::write('Session.start', true);
Configure::write('Session.checkAgent', false);
Configure::write('Security.level', 'low');
Here comes the first problem, on session.timwout it is specified as SECONDS but if I put 1 using low that gives my cookie expire of 5 hours (?). with this setting I am getting 3 minutes to cookie expire, I don't understand that, it should be 1 x 300 seconds = 5 minutes. what kind of math is that?
And the main problem is that this is not being respected, when I log to my website I can see it generated a session and will expire in 3 minutes, but as soon as I log on and click a link i get back to the log in page, which means I get de authenticated in less than 30 seconds.
I am trying to set such a low value for testing, I know high and medium security values regenerate session between requests but I would like to understand what's going on.
Thanks a lot.
Session lifetime and cookie lifetime aren't equal. Session lifetime is calculated by
Security::inactiveMins() * Configure::read('Session.timeout')
where as cookie lifetime is calculated by
Configure::read('Session.timeout') * (Security::inactiveMins() * 60)
So on a security level of low, a session timeout of 1 results in a session lifetime of 300 seconds, and a cookie lifetime of 18000 seconds, ie 5 hours.
And when using a 0.01 second timeout, session lifetime would be 3 seconds, and cookie lifetime would be 180 seconds, and therefore you are being logged out so fast.
As you've experienced for yourself, there's no need to worry about the longer cookie lifetime (which I guess is to prevent the cookie becoming invalid before the session times out, but I could be wrong on that), once the session times out, the cookie is being invalidated and finally overwritten.

Does the session garbage collector runs even if session is not idle?

I set in php.ini :
session.gc_probability = 100
session.gc_divisor = 100
session.gc_maxlifetime = 1040
So it is sure the session garbage collector will run after 1040 seconds. But what I want to know is that : does this session garbage collector run even if the user is not idle after creating a session ?
Every time user accessing the application the session file age will be reset to zero. So it will live at least next 1040 seconds.

How to make users not be logged out after certain time (PHP/APACHE)

I already made these adjustments in my php.in file and then stopped/started the server:
; 24 hour session cookie
session.cookie_lifetime = 86400
; Prevent server from cleaning up session
; Some value higher than the cookie lifetime
session.gc_maxlifetime = 200000
But that seemed to do nothing and my users are still complaining that they get logged out after about 30 minutes. And I am also getting logged out often.
What else could I look into or do in order to make my users who are logged in not to be logged out and keep them logged in at least 24 hours or more.
Thanks!
Whilst you can increase the session time out using code similar to the below: (in .htaccess, if you are on apache)
php_value session.gc_maxlifetime 86400
php_value session.gc_probability 1
php_value session.gc_divisor 100
The problem is that your sessions folder can become cluttered with inactive session files. Our sites use a half hour time out, but we have a an AJAX poller as part of the management interface which keeps the session alive once every 15 minutes. That way we only keep the session open for active users (even if they are perfoming long term operations)
Alternatively you may consider storing a separate - longer term - cookie which can be used to quickly re-establish the users session should it expire, again this prevents the need to fill your server with cumbersome session files.

Session timeouts in PHP: best practices

What is the actual difference between session.gc_maxlifetime and session_cache_expire() ?
Suppose I want the users session to be invalid after 15 minutes of non-activity (and not 15 after it was first opened). Which one of these will help me there?
I also know I can do session_set_cookie_params() which can set the user's cookie to expire in some amount of time. However, the cookie expiring and the actual session expiring on the server side are not the same; does this also delete the session when the cookie has expired?
Another solution I have though of is simple
$_SESSION['last_time'] = time()
on every request, and comparing the session to the current time, deleting the session based on that. I was hoping there was a more "built-in" mechanism for handling this though.
Thanks.
I spent some time looking for a good answer to how the php.ini server settings make
sessions expire. I found a lot of info but it took a while to figure out why
the settings work the way they do. If you're like me, this might be helpful to you:
Sessions are stored as cookies (files on the client's pc) or server side as files
on the server. Both methods have advantages and disadvantages.
For the sessions stored on the server, three variables are used.
session.gc_probability
session.gc_divisor
session.gc_maxlifetime
(session.gc_probability/session.gc_divisor) produces the probability that the
garbage collection routine will run. When the garbage collector runs, it
checks for session files that haven't been accessed for at least session.gc_maxlifetime
and deletes them.
This is all explained pretty well in forum posts (this one especially!) - But the
following questions do come up:
1.) How is that probability applied? When does the server roll the dice?
A: The server rolls the dice every time session_start() is called during
any active session on the server. So this means you should see the garbage
collector run roughly once for every 100 times that session_start() is called
if you have the default of session.gc_probability = 1 and session.gc_divisor = 100
2.) What happens on low volume servers?
A: When session_start() is called it FIRST refreshes the session and makes the
session values available to you. This updates the time on your session file on the
server. It THEN rolls the dice and if it wins (1 out of 100 chance) it calls the garbage collector. The garbage collector then checks all session id files and sees if there are
any that are eligible for deletion.
So this means that if you are the only person on the server, your session will
never go inactive and it will appear as though changing the settings have no
effect. Let's say you change session.gc_maxlifetime to 10 and session.gc_probability
to 100. This means there is a 100% chance the garbage collector will run and it
will clear out any session files that haven't been accessed in the last 10 seconds.
If you're the only one on the server, your session will not be deleted. You need
at least 1 other active session running for yours to go inactive.
So basically, on a low volume server or at a low volume time - it could be MUCH
longer than session.gc_maxlifetime before the garbage collector actually runs and
the sessions are actually deleted. And without knowing how this works, it may
appear completely random to you.
3.) Why do they use the probability?
A: Performance. On a higher volume server you don't want the garbage collector
running on every request of session_start(). It will slow down the server
needlessly. So depending on your server volume, you may want to increase
or decrease the probability that the garbage collector runs.
I hope that this ties things together for you. If you're like me and you tried
session.gc_maxlifetime and it didn't seem to work (because you tried it
out on a development server so as not to disturb anyone), then this post
hopefully saved you some head scratching.
Good luck!
Each time session_start is called the session files timestamp (if it exists) gets updated, which is used to calculated if session.gc_maxlifetime has been exceeded.
More importantly you can't depend on a session to expire after session.gc_maxlifetime time has been exceeded.
PHP runs garbage collection on expired sessions after the current session is loaded and by using session.gc_probability and session.gc_divisor it calculates the probability that garbage collection will run. By default its a 1% probability.
If you have a low number of visitors there is a probability that an inactive user could access a session that should have expired and been deleted. If this is important to you will need to store a timestamp in the session and calculate how log a user has been inactive.
This example replaces session_start and enforces a timeout:
function my_session_start($timeout = 1440) {
ini_set('session.gc_maxlifetime', $timeout);
session_start();
if (isset($_SESSION['timeout_idle']) && $_SESSION['timeout_idle'] < time()) {
session_destroy();
session_start();
session_regenerate_id();
$_SESSION = array();
}
$_SESSION['timeout_idle'] = time() + $timeout;
}
session.gc_maxlifetime is based off of the last time a session file was modified. So every time a session file is modified or a session_start() is called in a separate page, the countdown to gc_maxlifetime begins anew and the user stays "logged in". This is the value you are looking for. You can modify this through ini_set() in your php files, or edit php.ini if you have access to it
session_cache_expire() only controls the HTTP "Expires" header. This header controls how long the downloaded page contents stay in the user's browser cache.
To check the current values, this code will be helpful:
$gc_maxlifetime = ini_get('session.gc_maxlifetime');
$gc_probability = ini_get('session.gc_probability');
$gc_divisor = ini_get('session.gc_divisor');

Categories