0
0
PhpHow-ToBeginner · 3 min read

How to Set Cookie Expiration in PHP: Simple Guide

In PHP, you set cookie expiration by passing the expiration time as a Unix timestamp to the setcookie() function's third parameter. Use time() + seconds to specify how long the cookie should last, for example, time() + 3600 for one hour.
📐

Syntax

The setcookie() function sets a cookie with optional parameters. The third parameter is the expiration time, given as a Unix timestamp (seconds since January 1, 1970). If you want the cookie to expire in the future, add the number of seconds to time(). If you set it to 0 or omit it, the cookie expires when the browser closes.

  • name: The cookie's name.
  • value: The cookie's value.
  • expire: Expiration time as Unix timestamp.
  • path: Path on the server where the cookie is available.
  • domain: Domain that can access the cookie.
  • secure: If true, cookie sent over HTTPS only.
  • httponly: If true, cookie not accessible via JavaScript.
php
setcookie(string $name, string $value = "", int $expire = 0, string $path = "", string $domain = "", bool $secure = false, bool $httponly = false): bool
💻

Example

This example sets a cookie named user with value John that expires in 1 hour (3600 seconds). It shows how to set the expiration time using time() + 3600.

php
<?php
// Set cookie 'user' to 'John' that expires in 1 hour
setcookie('user', 'John', time() + 3600);

// Confirm cookie is set
if (isset($_COOKIE['user'])) {
    echo 'Cookie user is set with value: ' . $_COOKIE['user'];
} else {
    echo 'Cookie user is not set yet.';
}
?>
Output
Cookie user is not set yet.
⚠️

Common Pitfalls

Common mistakes when setting cookie expiration include:

  • Setting expiration time in the past or as a string instead of a Unix timestamp.
  • Calling setcookie() after outputting HTML or whitespace, which causes headers to be sent too late.
  • Forgetting that cookies set in the current request are not available until the next page load.

Always call setcookie() before any output and use time() + seconds for expiration.

php
<?php
// Wrong: expiration as string (does not work as expected)
setcookie('test', 'value', '3600'); // Wrong

// Right: expiration as Unix timestamp
setcookie('test', 'value', time() + 3600); // Correct

// Wrong: calling after output
echo 'Hello';
setcookie('test2', 'value2', time() + 3600); // Warning: headers already sent
?>
📊

Quick Reference

Summary tips for setting cookie expiration in PHP:

  • Use time() + seconds for expiration time.
  • Set expiration to 0 or omit for session cookies (expire on browser close).
  • Call setcookie() before any output.
  • Remember cookies are available on next page load, not immediately.

Key Takeaways

Use time() + seconds to set cookie expiration as a Unix timestamp.
Call setcookie() before sending any output to avoid header errors.
Cookies set in the current request are available only on the next page load.
Set expiration to 0 or omit it for session cookies that expire on browser close.
Always use integer timestamps, not strings, for the expiration parameter.