Skip to content

Web Nerdism w/ PHP 101 › Basics

Cookies

Phillip Harrington Nov 2026

HTTP forgets you. Every request stands on its own, so as far as the server knows, your second click comes from a stranger. And yet, sites keep you logged in, remember what's in your cart, and know you picked dark mode. That's cookies: a small piece of text the server asks your browser to keep, and to hand back with every request after.

A name and a value, like theme=dark, kept by the browser for one site. The server sets it with a response header:

Set-Cookie: theme=dark; Max-Age=31536000; Path=/; Secure; HttpOnly; SameSite=Lax

From then on, every request the browser makes to that site carries it back, in a request header:

Cookie: theme=dark

The trick: the server doesn't remember you; your browser reminds it, every single time.

  • Max-Age or Expires: how long to keep it. Leave both off, and it's a session cookie, gone when the browser closes.
  • Path and Domain: which URLs it gets sent to.
  • Secure: only send it over HTTPS.
  • HttpOnly: JavaScript in the page can't read it.
  • SameSite: whether other sites can make your browser send it. Lax is the sensible default.

The last three are security, and they get a lesson of their own, Sessions and Cookie Flags. For now: login cookies get all three.

How It Works

  1. You visit a site. The browser has no cookies for it, so it sends none.
  2. The server answers with a Set-Cookie header.
  3. The browser stores it, labeled with the site's name.
  4. Every later request to that site (pages, images, scripts, all of them) includes it in a Cookie header, until it expires.
BrowserServerGET /200 OK Set-Cookie: visits=1GET / Cookie: visits=1200 OK Set-Cookie: visits=2GET / Cookie: visits=2every requestcarries it back
The server never remembers. The browser reminds it, every time.

To delete a cookie, the server sets it again with an expiry in the past, and the browser throws it away. In PHP, below, shows the one line it takes.

Notice step four, above. The cookie comes from the browser, which means from whoever controls the browser. A cookie is input, and you never trust input. Anyone can open their browser's developer tools and change a cookie to anything they like. So a cookie should never say "role=admin" or "price=1.00".

The usual move is to put a long random ID in the cookie, and keep everything that matters on the server, filed under that ID. That's a session, and it gets a lesson of its own.

Try It

A visit counter that lives in a cookie. In a folder, make index.php:

<?php
$visits = (int) ($_COOKIE['visits'] ?? 0) + 1;

// Wants a cookie?
setcookie('visits', (string) $visits, [
    'expires' => time() + 3600,
    'httponly' => true,
    'samesite' => 'Lax',
]);

echo "Visit number $visits.\n";

cookies/index.php on GitHub

Start the server the usual way:

docker run --rm -it -p 8000:8000 -v "$PWD":/app -w /app php:8.4-cli php -S 0.0.0.0:8000

Open http://localhost:8000 and reload a few times. The number goes up: the browser is sending the cookie back, and the server adds one each time.

The page in a browser: Visit number 5.
A few reloads later.

Now break it. Open your browser's developer tools, find the cookies for localhost (the Application tab in Chrome, Storage in Firefox), and change visits to 41. Reload: "Visit number 42." Of course it is. You just told the server something false, and it believed you. That isn't a classroom trick, either: plenty of real break-ins started with exactly that move. Or skip the browser entirely, and send any cookie you like:

Chrome's developer tools, Application tab, Cookies, http://localhost:8000: one cookie, visits, with the value 1.
Application, then Cookies, then your site.
The visits cookie being edited in developer tools, its value changed to 41.
Double-click the value, and type whatever you like.
The page in a browser: Visit number 42.
Of course it is.
curl -b 'visits=41' http://localhost:8000

New here:

-b 'visits=41'
Send this cookie with the request, the way a browser would: name, equals sign, value.
Terminal: curl -b 'visits=41' http://localhost:8000 prints Visit number 42.
No browser, no cookie jar. Just a claim.

Try -b 'visits=banana', too. The (int) in the code, a cast, turns nonsense into 0, so you get visit 1, not an error. That one cast is the code not trusting its input.

Terminal: curl -b 'visits=banana' http://localhost:8000 prints Visit number 1.
Bananas count as zero.

New to Docker or the terminal? Tools has the setup.

In PHP

  • setcookie($name, $value, $options) sends Set-Cookie. The options array takes expires, path, domain, secure, httponly and samesite.
  • $_COOKIE holds the cookies the browser sent with this request. Treat every value in it as input from a stranger, because it is.
  • To delete one: setcookie('visits', '', ['expires' => time() - 3600]);

Gotchas

  • A cookie you just set isn't in $_COOKIE yet.setcookie() only adds a header to the response. The browser sends it back starting with the next request.
  • "Headers already sent." A cookie is a header, so setcookie() has to come before any output, like header() in Request and Response.
  • Cookies ride along on everything. Every image, script and stylesheet request to the site carries them too. Keep them small: a browser keeps about 4 KB per cookie, about a page of text, and all of it rides along with every image on the page.
  • Never trust a cookie. It's input. It can be edited, replayed, or made up from scratch, as Try It showed.

A private window is a fresh cookie jar

Need to see your site as a stranger, logged out, nothing remembered? Open a private window. It starts with no cookies and forgets them all when you close it, so you don't have to clear anything from your real browser.

A cookie rides along in a header. There's an even plainer way to send a little data with a request, right in the URL. Next: query strings.