Skip to content

PHP Tutorial Sessions and Cookies

Reading Cookies

Lesson 182 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

Cookies sent by the browser arrive in $_COOKIE. Reading them is easy. Reading them safely takes a little care.

Check that it exists

index.php
PHP
<?php
$_COOKIE = ["lang" => "hi"];

if (isset($_COOKIE["lang"])) {
    echo "Language cookie: " . $_COOKIE["lang"];
} else {
    echo "No language chosen";
}
Output
OUTPUT
Language cookie: hi

Shorter, with a default:

index.php
PHP
<?php
$_COOKIE = [];

$lang = $_COOKIE["lang"] ?? "en";

echo $lang;
Output
OUTPUT
en

Validate the value

The value is whatever the browser sent. Compare it with the values you accept.

index.php
PHP
<?php
$_COOKIE = ["lang" => "../../etc"];

$supported = ["en", "hi", "ta"];
$lang = in_array($_COOKIE["lang"] ?? "", $supported, true) ? $_COOKIE["lang"] : "en";

echo $lang;
Output
OUTPUT
en

Numbers

index.php
PHP
<?php
$_COOKIE = ["font_size" => "99999"];

$size = filter_var($_COOKIE["font_size"] ?? 16, FILTER_VALIDATE_INT, [
    "options" => ["min_range" => 12, "max_range" => 32, "default" => 16],
]);

echo $size;
Output
OUTPUT
16

Escape on output

If you print a cookie value into HTML, escape it.

index.php
PHP
<?php
$_COOKIE = ["name" => '"><script>alert(1)</script>'];

echo htmlspecialchars($_COOKIE["name"] ?? "");
Output
OUTPUT
&quot;&gt;&lt;script&gt;alert(1)&lt;/script&gt;

Array cookies

A cookie named prefs[color] becomes a nested array in $_COOKIE. A visitor can also send an array where you expect a string. Check with is_string() first.

Cookies with the same name

If a browser sends two cookies with the same name (from different paths), PHP uses only one of them. If you see strange behaviour, check the path and domain of each cookie in the developer tools.

  • The cookie has expired or the user deleted it.
  • The path or domain does not match the page.
  • secure is set, but the page uses plain HTTP.
  • Cookies are blocked in the browser.
  • The cookie was set on this same request (it arrives on the next one).
lightbulb
Treat cookies like form input
They come from the visitor. Validate them, and fall back to a safe default when anything looks wrong.