PHP - Cookie expires after 48h

Asked

Viewed 12 times

0

Good afternoon,

I am having a problem using Cookies in PHP, I am using two cookies, one that defines the user and the other that defines the language used, however, since I started using the second cookie, both cookies are ending after 48h, even setting longer periods (30d-365d).

Follow an example of the code:

setcookie("id_usuario", $id_usuario, time() + (86400 * 30), "/");
setcookie("lang", "$lang", time() + (86400 * 30), "/");

Does anyone have any idea why cookies are clearing in 48 hours and/or a better way to maintain more than one cookie for long periods? my users are complaining a lot of having to relocate every 2 days.

Thanks.

  • Basically it’s a lack of understanding of PHP sessions. Cookie is just a "bearer" of identification, just changing its lifespan won’t help. You need to increase the entire session time (including the file that is generated). You have to be careful not to fill the open session server for nothing, in this case (for example, completely disabling the session time, something I’ve seen people doing).

  • Here is one of the factors that it is essential to adjust: https://www.php.net/manual/en/session.configuration.php#ini.session.gc-maxlifetime - without this, the cookie can last forever, but when the access is done it will not be useful for anything, because the session will already be deleted.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.