Questions And Answers

More Tutorials

PHP Cookies

The value of a cookie can be modified by resetting the cookie
setcookie("user", "John", time() + 86400, "/");

Cookies are part of the HTTP header, so setcookie() must be called before any output is sent to the browser.

When modifying a cookie make sure the path and domain parameters of setcookie() matches the existing cookie or a new cookie will be created instead.

The value portion of the cookie will automatically be urlencoded when you send the cookie, and when it is received, it is automatically decoded and assigned to a variable by the same name as the cookie name

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, "/");


  • 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.

    Checking if a Cookie is Set

    Use the isset() function upon the superglobal $_COOKIE variable to check if a cookie is set.
    // PHP <7.0
    if (isset($_COOKIE['user'])) {
     // true, cookie is set
     echo 'User is ' . $_COOKIE['user'];
    else {
     // false, cookie is not set
     echo 'User is not logged in';
    // PHP 7.0+
    echo 'User is ' . $_COOKIE['user'] ?? 'User is not logged in';

    Removing a Cookie

    To remove a cookie, set the expiry timestamp to a time in the past. This triggers the browser's removal mechanism:
    setcookie('user', '', time() - 3600, '/');

    When deleting a cookie make sure the path and domain parameters of setcookie() matches the cookie you're trying to delete or a new cookie, which expires immediately, will be created.

    It is also a good idea to unset the $_COOKIE value in case the current page uses it:

    Retrieving a Cookie

    Retrieve and Output a Cookie Named user

    The value of a cookie can be retrieved using the global variable $_COOKIE. example if we have a cookie named user we can retrieve it like this
    echo $_COOKIE['user'];


    In this page (written and validated by ) you learned about PHP Cookies . What's Next? If you are interested in completing PHP tutorial, your next topic will be learning about: PHP JSON.

    Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

    Share On:

    Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.