Skip to content
PHP Logo

What is the correct way to set a PHP cookie with an expiration time?

When setting a PHP cookie with an expiration time, it is important to specify the expiration time in Unix timestamp format. This ensures that the cookie will expire at the correct time. To set a cookie with an expiration time, you need to use the `setcookie()` function with the `time()` function to calculate the expiration time.

php
// Set a cookie with an expiration time of 1 hour
$expiration_time = time() + 3600; // current time + 1 hour
setcookie('cookie_name', 'cookie_value', $expiration_time);