Home >Backend Development >PHP Tutorial >How to Create a Practically Never-Expiring Cookie in PHP?

How to Create a Practically Never-Expiring Cookie in PHP?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-30 05:51:181015browse

How to Create a Practically Never-Expiring Cookie in PHP?

How to Set a Cookie to Never Expire

PHP's cookie documentation allows you to specify an expiration date for cookies, but it doesn't provide a clear way to make them persist indefinitely. However, there are ways to approximate this behavior.

Using a Far Future Date

The simplest method is to set a distant future date for the cookie's expiration. For instance, you can set a cookie that expires in a decade:

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

Note that setting a date past 2038 in 32-bit PHP will result in a cookie that expires immediately due to number wraparound.

Browser Limits

In 2023, most web browsers obey the maximum cookie expiration date, which varies depending on the browser. As of Chrome release M104, cookies cannot have an expiration date more than 400 days in the future.

The above is the detailed content of How to Create a Practically Never-Expiring Cookie in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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