PHP Cookies Setting a Cookie


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.