# HttpOnly Cookie

> A cookie setting. Scripts on the page can never read the cookie.

**Field:** Software Development  
**Category:** Security  
**Also known as:** HttpOnly, HttpOnly Flag, HttpOnly Attribute

## Think of it like

Think of it like a sealed letter. The postman can carry it, but he cannot read it.

## Definition

HttpOnly is a setting on a cookie that hides it from scripts on the page. The browser still sends the cookie with each request to its site. But code in the page cannot read it. So if an attacker gets a script onto the page through XSS, it cannot steal the login cookie. Login cookies should always carry it, along with Secure.

## Example

A bad comment on Jack's site runs a script that tries to read each reader's cookies. The login cookie is HttpOnly, so the script gets nothing it can use.

## Example code

```http
Set-Cookie: session=7f3a9c; HttpOnly; Secure
```

HttpOnly hides the cookie from page scripts. Secure sends it only over HTTPS.

## Why it matters

It keeps the login safe even when a script gets in. The script can act on the page, but cannot carry the login away.

## Common mistake

Thinking HttpOnly stops XSS. The script still runs, and it can act as the user while the page is open.

## Three principles

- **Hidden from scripts.** Page code cannot read it.
- **Still sent.** The browser adds it to requests.
- **Not a cure.** XSS still needs output encoding.

## Related terms

- [Session Hijacking](https://learn.benibix.com/term/development/session-hijacking)
- [Cross-Site Scripting (XSS)](https://learn.benibix.com/term/development/cross-site-scripting)
- [SameSite Cookie Attribute](https://learn.benibix.com/term/development/samesite-cookie)
- [HTTP Cookie](https://learn.benibix.com/term/development/cookie)
- [Output Encoding](https://learn.benibix.com/term/development/output-encoding)

## Told apart from

- [HttpOnly vs SameSite](https://learn.benibix.com/compare/httponly-vs-samesite): People think HttpOnly stops forged requests, and it does not. HttpOnly hides a cookie from scripts on the page, so an XSS script cannot steal the login. The browser still sends the cookie everywhere it goes. SameSite decides where the cookie goes. It keeps the cookie off requests that start on other sites, which is what stops CSRF. A login cookie wants both.

![Three panels. Brute force: guesses like 123456, password and qwerty are tried on one account until one works; stop it by limiting tries. Credential stuffing: a pair leaked from a games site, jack@example.com and sunset77, is tried on his shop account and works because he used it twice; stop it with a second factor. Session hijacking: Lucy logs in and gets a session cookie, a script steals it and the site sees Lucy with no password needed; stop it with HttpOnly cookies.](https://learn.benibix.com/diagrams/dev2-login-attacks.svg)

---

Source: https://learn.benibix.com/term/development/httponly-cookie
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
