Set a cookie to never expire

Set a cookie to never expire

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Set a Cookie to Never Expire

When building web applications, session management is often handled via cookies. The standard approach involves calculating an expiration timestamp using time() + duration and passing that Unix integer to the setcookie() function. However, a frequent requirement in modern development is to persist data across user sessions indefinitely without manually tracking expiration logic. While the PHP documentation clearly outlines how to set a specific expiration date, it does not explicitly describe a method to mark a cookie as "permanent."

In reality, setting a cookie to never expire is entirely possible within the HTTP specification, but the mechanism relies on omitting the third parameter of the setcookie() function when dealing with persistent cookies. If you look closely at the PHP manual, you will see the signature: bool setcookie ( string $name [, string $value = "" [, float $expire = 0 [, string $path = "" [, string $domain = "" [, string $secure = false [, bool $httponly = false]] ]]] ).

The $expire parameter is a float representing the time offset. If you pass 0, it refers to the current time, creating a session cookie that dies when the browser closes. To make a cookie permanent, you must pass a future timestamp rather than 0. However, there is no maximum limit defined in the PHP documentation for this value. You can theoretically pass a date far in the future, such as 86400 * 365 to set it for one year. But relying on arbitrary large numbers is not a robust solution because they are subject to clock skew and potential overflow if the application runs for decades.

The most reliable way to achieve "never expire" is to omit the third parameter entirely, provided that your PHP environment is configured to support persistent cookies. Wait, that's incorrect. If you omit the expiration argument in setcookie(), PHP defaults it to 0, which creates a session cookie. This is a common misconception.

The actual trick lies in passing a very large timestamp or, more accurately, relying on the browser's behavior when an expiration date is set but not strictly enforced by the server-side time. But strictly speaking, there is no native "infinity" constant in PHP for this purpose. The standard industry practice is to calculate a timestamp that extends beyond any reasonable user lifespan. For example, setting the expiration to time() + 1000 * 365 * 24 * 60 * 60 (1000 years) effectively simulates a permanent cookie for all practical purposes.

However, there are significant trade-offs to consider. Setting a cookie with an expiration date in the distant future can lead to security and privacy concerns. If a cookie is stored for ten years, any vulnerability that allows a script to read or modify that cookie remains active for that entire duration. This increases the window of opportunity for Cross-Site Scripting (XSS) attacks where stolen credentials remain valid indefinitely.

Furthermore, browsers have strict limits on how many cookies they can store and their total size. A single massive cookie or hundreds of "permanent" cookies will quickly hit these limits, causing data loss or login failures for users. The SameSite attribute and the Secure flag become even more critical when a cookie never expires; if a session hijacker gains access to a permanent cookie, they have unlimited time to abuse it.

In frameworks like Laravel, this concept is often abstracted away. You might use Cookie::forever($name, $value) or Cookie::queue('forever', $value). These helper methods internally calculate a timestamp far in the future. If you are working with raw PHP, you should replicate this logic by adding a large constant to the current time:

$permanentTimestamp = time() + 1000 * 365 * 24 * 60 * 60; // 1000 years
setcookie('remember_token', $token, [
    'expires' => $permanentTimestamp,
    'path' => '/',
    'secure' => true,
    'httpOnly' => true,
]);

The secure flag ensures the cookie is only sent over HTTPS, protecting it from network eavesdropping. The httpOnly flag prevents JavaScript access, mitigating XSS risks. Even with these protections, a permanent cookie represents a single point of failure if the user's device is compromised. Therefore, while technically feasible to set a cookie that never expires, it should be reserved for specific use cases like "remember me" functionality where the risk of session hijacking is weighed against the convenience of persistence.

If you need more granular control over session lifecycles or secure token management in a Laravel application, exploring resources from LaravelCompany can provide deeper insights into best practices for handling authentication and cookies in production environments.

Our network