Cookies
A cookie is a small value the server asks the browser to keep and to send back with the next requests. In PHP the script sets it with a header and reads it from the $_COOKIE array.
Set a cookie
Call setcookie() before any output to store a small value in the browser.
The browser then sends that value back on every later request to a matching path. Background in
PHP cookies.
Keep cookies tiny and never store passwords or permissions inside them.
The file set.php in the folder you serve with php -S 127.0.0.1:8000:
<?php setcookie("theme", "dark"); echo "preference saved";
curl -s "http://127.0.0.1:8000/set.php"
preference saved
The body is the visible part; the same response carries a Set-Cookie: theme=dark header, checked with a client that reads headers. The browser stores that header and offers the value again later.
Read $_COOKIE
On the next request PHP fills $_COOKIE with values the browser chose to send.
A cookie set in the current request is not yet visible there.
Always provide a default, since users can clear or block cookies at any time.
The file read.php in the same folder:
<?php $theme = $_COOKIE["theme"] ?? "light"; echo $theme;
curl -s -b "theme=dark" "http://127.0.0.1:8000/read.php"
dark
The request carries the cookie and the script answers with the stored value. A request without the cookie takes the default of light, and a blocked cookie never reaches the array at all.
Expiry, path and flags
The options array controls lifetime, scope, and safety flags. Set an expiry timestamp for
persistence, a path for scope, and secure flags for protection.
Modern defaults favor httponly and samesite protections.
The file params.php in the same folder:
<?php setcookie("theme", "dark", [ "expires" => time() + 60 * 60 * 24 * 30, "path" => "/", "secure" => true, "httponly" => true, "samesite" => "Lax", ]);
The response carries one header: theme=dark with Max-Age=2592000, path=/, secure, HttpOnly and SameSite=Lax. The Max-Age is the thirty days of the expression in seconds, the path opens the whole site, and the flags keep the value away from scripts and from plain connections. The header was read with a client that prints it.
Delete a cookie
There is no separate delete call. Send the same cookie again with an expiry time in the past
and an empty value, using the same path and domain as when it was created.
After deletion the browser stops sending the value on the following requests.
The file delete.php in the same folder:
<?php setcookie("theme", "", [ "expires" => time() - 3600, "path" => "/", ]); echo "preference cleared";
curl -s "http://127.0.0.1:8000/delete.php"
preference cleared
The header of that response is theme=deleted; Max-Age=0; path=/, and the browser drops the value it held. The empty value and the past expiry are one instruction to the browser, not two.
Exercises
- Set a second cookie called lang in set.php and print both values in read.php.
- Change the path of the theme cookie to /dev/php/ and see which requests still carry it.
- Request read.php with a forged cookie value and print what the script does with it.
- Add samesite Strict to the delete call and compare the header.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Where does a cookie live?
Answer
In the browser
The server only sends the value; the browser stores it and sends it back with later requests.
-
What must come before
setcookie()?Answer
No output at all
The cookie travels in a header, and PHP cannot add a header after the body has started.
-
A cookie is set during the current request. When does it reach
$_COOKIE?Answer
With the next request
The current request was parsed before the cookie existed, so the browser sends it on the next one.
-
How do you delete a cookie?
Answer
Send it again with an empty value and an expiry in the past
The browser drops a cookie whose expiry has passed, so the same name with a past time removes it.
-
What does this program print?
<?php $cookie = ["theme" => "dark"]; echo $cookie["theme"] ?? "light";Answer
dark
The key exists and is not null, so the left side of the null coalescing operator wins.
-
What does this program print?
<?php $cookie = ["theme" => "dark"]; $theme = $cookie["theme"] ?? "light"; echo strtoupper($theme);Answer
DARK
The stored value wins and strtoupper turns it into upper case.
-
What does this program print?
<?php $cookie = ["theme" => null]; echo $cookie["theme"] ?? "light";Answer
light
The null coalescing operator treats a null value as missing and takes the right side.
-
What does this program print?
<?php echo 60 * 60 * 24 * 30;Answer
2592000
Thirty days of seconds: 60 times 60 times 24 times 30.
-
Why is a password in a cookie a bad idea?
Answer
The user can read and change it, and it travels with every request
A cookie is data of the client, not a secret of the server, so it can be inspected and forged.
-
What does the
httponlyflag do?Answer
Hides it from JavaScript
A script in the page cannot read a cookie marked HttpOnly, which limits a cross site scripting attack.
The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.
Article author: Arthur Isaev