PHP Cookies Setting a Cookie

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!


A cookie is set using the setcookie() function. Since cookies are part of the HTTP header, you must set any cookies before sending any output to the browser.


setcookie("user", "Tom", time() + 86400, "/"); // check syntax for function params


  • Creates a cookie with name user
  • (Optional) Value of the cookie is Tom
  • (Optional) Cookie will expire in 1 day (86400 seconds)
  • (Optional) Cookie is available throughout the whole website /
  • (Optional) Cookie is only sent over HTTPS
  • (Optional) Cookie is not accessible to scripting languages such as JavaScript

A created or modified cookie can only be accessed on subsequent requests (where path and domain matches) as the superglobal $_COOKIEis not populated with the new data immediately.

Got any PHP Question?