Home > Backend Development > PHP Tutorial > Can Cookies Truly Never Expire?

Can Cookies Truly Never Expire?

Linda Hamilton
Release: 2024-11-25 07:57:10
Original
391 people have browsed it

Can Cookies Truly Never Expire?

Understanding the Expirability of Cookies

Cookies are essential for storing information on a user's device, allowing websites to provide personalized experiences. However, the question arises: Can we set cookies to never expire?

Expiration Mechanisms

All cookies follow a defined lifespan, as set by the HTTP cookie specification. By default, session cookies expire when the browser closes, while persistent cookies can have expiration dates in the future.

Setting a Never-Expiring Cookie

Technically, it is not possible to completely prevent the expiration of cookies. However, we can achieve practical perpetual cookie existence by setting a far-future expiration date.

Ex: A Cookie with a 10-Year Expiration Date

To set a cookie that expires in approximately ten years:

setcookie(
  "CookieName",
  "CookieValue",
  time() + (10 * 365 * 24 * 60 * 60)
);
Copy after login

Browser Limitations

Note that in 32-bit PHP systems, setting an expiration date beyond 2038 may result in an instant expiration due to number wrap-around.

Web Browser Restrictions (Chrome Update)

As of Chrome release M104 (August 2022), the maximum expiration limit for cookies has been set at 400 days in the future. This limitation may be adopted by other browsers in the future, effectively limiting cookie persistence.

The above is the detailed content of Can Cookies Truly Never Expire?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template