Skip to content
PHP Logo

What potential pitfalls should be considered when using setcookie() in PHP?

One potential pitfall when using setcookie() in PHP is not setting the expiration time correctly. If the expiration time is not set, the cookie will only last for the duration of the session, which may not be desired. To ensure the cookie persists beyond the session, set an appropriate expiration time using the time() function.

php
// Set a cookie with an expiration time of 1 hour
setcookie("cookie_name", "cookie_value", time() + 3600);