The session in PHP consists of a cookie with an identifier, and a server-side cleaning system.
Client side
The time of cookie of the current session from the beginning this, and even if the person continues to access the site, the session will expire at the time stipulated in the preferences, without being automatically renewed.
If you want to extend the session so that the time runs from the last access, and not from the first, you need to renew the cookie session periodically (or each access).
It has several ways, this is of the own PHP manual:
<?php
$lifetime=600;
session_start();
setcookie(session_name(),session_id(),time()+$lifetime);
?>
Another one, taken out of this website is also a good example:
Setcookie(
ini_get("session.name"),
session_id(),
time()+ini_get("session.cookie_lifetime"),
ini_get("session.cookie_path"),
ini_get("session.cookie_domain"),
ini_get("session.cookie_secure"),
ini_get("session.cookie_httponly")
);
Server side
Do not forget that anyway, this value of the php.ini
It needs to be longer than session time. If you have a 2 hour session, use for example 7800 ( 2h * 60m * 60s = 7200, plus 600 to give 10 minutes of "time off"):
In the php.ini
:
session.gc_maxlifetime = 7800
You can even use ini_set
, But if you have any other page using cookie, with a shorter time of gc, nothing will help, because it will count the shorter time. If you don’t have access to php.ini
should set the gc_maxlifetime
and the session_save_path
I have now seen the detail of the update issue. If it were with
session_set_cookie_params
i could say that after the session startup, the defined value will be evaluated within the stipulated period, and would not be "renewed" when the user updates the browser.– Wallace Maxters
The manual does not say anything about it, but I believe that because it is a function, the value is overwritten every time.
– Phiter
Related: Session with a duration of 30 minutes
– Wallace Maxters
@Wallacemaxters my question continues, if I load the page the session gains 30 more minutes?
– Hugo Borges
I had made an answer, but I gave up. I don’t know much about these HTTP subjects to explain in depth. But the function you are using will only change the lifespan of the server cache, you have to tinker with the Cookie as well. With my knowledge, if you touch the cookie, through
session_cookie_params
the time is only counted from the creation of the session. Refresh does not affect the count.– Wallace Maxters
In fact the time keeps up. It is relative to Session, regardless of F5.
– Gabriel Gomes