Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Variables and Constants

PHP $_COOKIE superglobal

Reference for PHP $_COOKIE: read the cookies a browser sends back, why setcookie() does not change it right away, and how to treat cookie values as untrusted.

$_COOKIE is an associative array of the cookies that the browser sent with the current request, taken from the Cookie header. It contains names and values only; the expiry date, path and flags you chose when setting the cookie are never sent back.

You create cookies with setcookie(), which writes a Set-Cookie header into the response. That does not touch $_COOKIE: the new cookie shows up in the array on the next request, after the browser sends it back.

PHP
<?php
$theme = $_COOKIE["theme"] ?? "light";

Try it

A command-line script has no browser, so the first example fills $_COOKIE the way PHP does from a header. The second shows what setcookie() does and does not change.

PHP Parsing a Cookie header by hand

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP setcookie() does not change $_COOKIE

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
$_COOKIE
$_COOKIE["name"]

Type: array<string, string|array>

Names with square brackets (user[name]) become nested arrays, like query parameters.

Usage notes

  • Cookies are user input. A visitor can edit, delete or forge any cookie. Never store prices, roles or "is_admin" flags in a plain cookie. Keep sensitive state in $_SESSION and put only the random session ID in a cookie.
  • Values are strings. Even a number is stored as text; the visits cookie above arrives as "3".
  • Timing. setcookie() affects the next request. If you need the value right now, store it in a variable too.
  • Names are rewritten. Spaces and dots in names turn into underscores (my.cookie becomes my_cookie), as for query parameters.
  • Size and number. Browsers allow roughly 4 KB per cookie and a limited number per domain. Cookies are sent with every request, including images and scripts, so large cookies slow a site down.
  • Flags matter. HttpOnly hides the cookie from JavaScript, Secure restricts it to HTTPS, and SameSite limits cross-site sending. Set them in setcookie().
  • Delete with the same path and domain. A cookie is identified by name, path and domain. Using different values leaves the original in place.
  • Do not unserialize cookie values. unserialize($_COOKIE["x"]) on untrusted data is a code-injection risk. Use json_decode() and validate the result.
  • Privacy law. Tracking cookies usually need consent in the EU and other regions.
  • Not in $_REQUEST by default. The sample production ini file leaves cookies out of $_REQUEST.