I would like to extend the session timeout in php
I know that it is possible to do so by modifying the php.ini file. But I don't have access to it.
So is it possible to do it only with php code?
I would like to extend the session timeout in php
I know that it is possible to do so by modifying the php.ini file. But I don't have access to it.
So is it possible to do it only with php code?
No. If you don't have access to the php.ini, you can't guarantee that changes would have any effect.
I doubt you need to extend your sessions time though.
It has pretty sensible timeout at the moment and there are no reasons to extend it.
Session timeout is a notion that has to be implemented in code if you want strict guarantees; that's the only way you can be absolutely certain that no session ever will survive after X minutes of inactivity.
If relaxing this requirement a little is acceptable and you are fine with placing a lower bound instead of a strict limit to the duration, you can do so easily and without writing custom logic.
Convenience in relaxed environments: how and why
If your sessions are implemented with cookies (which they probably are), and if the clients are not malicious, you can set an upper bound on the session duration by tweaking certain parameters. If you are using PHP's default session handling with cookies, setting
session.gc_maxlifetime
along withsession_set_cookie_params
should work for you like this:This works by configuring the server to keep session data around for at least one hour of inactivity and instructing your clients that they should "forget" their session id after the same time span. Both of these steps are required to achieve the expected result.
If you don't tell the clients to forget their session id after an hour (or if the clients are malicious and choose to ignore your instructions) they will keep using the same session id and its effective duration will be non-deterministic. That is because sessions whose lifetime has expired on the server side are not garbage-collected immediately but only whenever the session GC kicks in.
GC is a potentially expensive process, so typically the probability is rather small or even zero (a website getting huge numbers of hits will probably forgo probabilistic GC entirely and schedule it to happen in the background every X minutes). In both cases (assuming non-cooperating clients) the lower bound for effective session lifetimes will be
session.gc_maxlifetime
, but the upper bound will be unpredictable.If you don't set
session.gc_maxlifetime
to the same time span then the server might discard idle session data earlier than that; in this case, a client that still remembers their session id will present it but the server will find no data associated with that session, effectively behaving as if the session had just started.Certainty in critical environments
You can make things completely controllable by using custom logic to also place an upper bound on session inactivity; together with the lower bound from above this results in a strict setting.
Do this by saving the upper bound together with the rest of the session data:
Session id persistence
So far we have not been concerned at all with the exact values of each session id, only with the requirement that the data should exist as long as we need them to. Be aware that in the (unlikely) case that session ids matter to you, care must be taken to regenerate them with
session_regenerate_id
when required.Adding comment for anyone using Plesk having issues with any of the above as it was driving me crazy, setting session.gc_maxlifetime from your PHP script wont work as Plesk has it's own garbage collection script run from cron.
I used the solution posted on the link below of moving the cron job from hourly to daily to avoid this issue, then the top answer above should work:
https://websavers.ca/plesk-php-sessions-timing-earlier-expected
If you use PHP's default session handling, the only way to reliably change the session duration in all platforms is to change php.ini. That's because in some platforms, garbage collection is implemented through a script that runs every certain time (a cron script) that reads directly from php.ini, and therefore any attempts at changing it at run time, e.g. via
ini_set()
, are unreliable and most likely won't work.For example, in Debian Linux systems, garbage collection is done via /etc/cron.d/php5 which runs at XX:09 and XX:39 (that is, every half hour), and if it finds a session older than the session.gc_maxlifetime specified in php.ini, then that session is deleted without mercy. That also explains why in this question: PHP sessions timing out too quickly, the OP had problems in one host but the problems ceased when switching to a different host.
So, given that you don't have access to php.ini, if you want to do it portably, using the default session handling is not an option. Apparently, extending the cookie lifetime was enough for your host, but if you want a solution that works reliably even if you switch hosts, you have to use a different alternative.
Available alternative methods include:
Set a different session (save) handler in PHP to save your sessions in a different directory or in a database, as specified in PHP: Custom Session Handlers (PHP manual), so that the cron job doesn't reach it, and only PHP's internal garbage collection takes place. This option probably can make use of
ini_set()
to set session.gc_maxlifetime but I prefer to just ignore the maxlifetime parameter in mygc()
callback and determine maximum lifetime on my own.Completely forget about PHP internal session handling and implement your own session management. This method has two main disadvantages: you will need your own global session variables, so you lose the advantage of the
$_SESSION
superglobal, and it needs more code thus there are more opportunities for bugs and security flaws. Most importantly, the session identifier should be generated out of cryptographically secure random or pseudorandom numbers to avoid session ID predictability (leading to possible session hijacking), and that is not so easy to do with PHP portably. The main advantage is that it will work consistently in all platforms and you have full control over the code. That's the approach taken e.g. by the phpBB forum software (at least version 1; I'm not sure about more recent versions).There is an example of (1) in the documentation for
session_set_save_handler()
. The example is long but I'll reproduce it here, with the relevant modifications necessary to extend the session duration. Note the inclusion ofsession_set_cookie_params()
to increase the cookie lifetime as well.Approach (2) is more complicated; basically, you have to re-implement all session functions on your own. I won't go into details here.
You can override values in php.ini from your PHP code using
ini_set()
.Put
$_SESSION['login_time'] = time();
into the previous authentication page. And the snipped below in every other page where you want to check the session time-out.