PHP - correctly set cookie parameters (expire) using header() - not set-cookie()

PHP - correctly set cookie parameters (expire) using header() - not set-cookie()

August 14, 2026 · Illustrated author avatarBy Stefan Bogdanescu
PHP - correctly set cookie parameters (expire) using header() - not set-cookie()

PHP - Correctly Setting Cookie Parameters (Expire) Using header()

Introduction

In this post, we’ll address an issue related to setting cookie parameters using the header() function in PHP, specifically focusing on the correct format for the expires parameter. This problem arises when working with older versions of HHVM that don’t support the $options array available in PHP 7.3 or above.

Understanding the Issue

The questioner is dealing with a Magento 1 site running an older version of HHVM (3.30+) where upgrading would break everything on the site. The setcookie() function, which provides a more flexible way to set cookies, doesn’t support the $options array in this context.

Setting Cookies Using header()

The developer has managed to set a cookie using the header() function by concatenating all the required properties:

header('Set-Cookie: frontend=abcdef; expires=188888888; path=/; domain=www.mydomain.com; SameSite=None; Secure');

The resulting header appears in the response as:

frontend=abcdef; expires=188888888; path=/; domain=www.mydomain.com; Secure; SameSite=None

The Question - Setting the Expires Value

The key question here is how to correctly set the expires value when using the raw header() function instead of setcookie(). Since PHP automatically converts the Unix timestamp internally for setcookie(), we need to ensure we’re providing the expires parameter in the correct format.

The Format Required for Expires

The PHP documentation suggests that the expires parameter takes on a Unix timestamp. However, it’s important to understand that PHP does this conversion internally when using setcookie().

To determine if our current approach is correct, let’s analyze some sample code:

$b = 3600 * 24 * 365;
$c = time() + $b;

echo $c; // Output: 1643355613

$dt = new DateTime();
$dt->setTimestamp($c);
echo $dt->format('Y-m-d H-i-s'); // Output: 2022-01-28 09-40-13

In this example, we calculate a Unix timestamp representing the current time plus one year. We then convert this timestamp to a formatted date string using DateTime.

The Proper Format for Expires

The questioner is unsure if the format generated by DateTime->format('Y-m-d H-i-s') will be correctly used by the expires parameter.

However, according to PHP documentation on setcookie(), PHP internally converts a Unix timestamp into the required date format for the expires parameter:

You may notice the expires parameter takes on a Unix timestamp, as opposed to the date format Wdy, DD-Mon-YYYY HH:MM:SS GMT, this is because PHP does this conversion internally.

Therefore, when using header() directly, providing a Unix timestamp should be sufficient for setting the expires value in the cookie header. The HHVM version used here seems to support this format as demonstrated by the successful creation of the cookie header.

Conclusion

In summary:

  1. When working with older versions of HHVM and unable to use setcookie(), you can set cookies using the raw header() function.
  2. To set the expires value correctly, provide a Unix timestamp in the format generated by PHP’s time() function.
  3. The format generated by DateTime->format('Y-m-d H-i-s') is not required for setting the expires value; PHP converts this internally when using setcookie().

For more detailed information on cookie parameters and best practices, you can refer to our guide on working with cookies in Laravel applications at LaravelCompany.

Remember that cookie settings should be carefully managed to ensure both security and usability for your users. Always test changes thoroughly before deploying them to a production environment.

Magento 1 Configuration Errors

Our network