Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

A cookie is data the browser stores and sends back to a server on matching requests. A session is application state kept across those requests. A JWT is a compact, URL-safe format for representing claims. Because they sit at different layers, they are not competing options: a session identifier or a JWT can itself be carried inside a cookie, and that combination is common.

Three terms, three layers

Cookie: a storage and transport mechanism

Cookies are defined in RFC 6265, “HTTP State Management Mechanism,” published by the IETF in April 2011. A server sends a Set-Cookie response header; the browser (the user agent) stores the value and its attributes, then returns the value in a Cookie request header on later requests that match the cookie’s rules. A cookie does not, on its own, make anyone logged in. Its Expires and Max-Age attributes control how long the browser keeps it, but they do not by themselves decide whether the application still accepts the credential the cookie carries.

Session: application state across requests

HTTP requests do not remember earlier requests, so applications keep their own record of a client’s state. That record is the session. RFC 6265 describes the common arrangement: the server stores state and hands the browser a random, opaque identifier, usually inside a cookie. On each request the server reads the identifier and uses it as a key to find the stored state. Where the session data lives, when it expires, and how it is deleted are decisions the application makes.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

JWT: a token format

JSON Web Token is defined in RFC 7519, published by the IETF in May 2015. It describes how a set of claims (statements such as who the subject is and when the token expires) is encoded as a compact string that can appear in a URL, a header, or a cookie. The format specifies how the token is protected, not where it is kept or how an application uses it. A JWT is therefore a token, not a session and not a cookie.

How the three compare

Question Cookie Server-managed session JWT
What it is HTTP mechanism for storing data in the browser and returning it to the server Application state kept across requests Compact, URL-safe representation of claims
Where the data lives The value and attributes are held by the browser; the server receives the value on applicable requests Usually on the server; the client holds only an identifier In the token itself; where the token is stored is an application decision
How it reaches the server Sent automatically in the Cookie header on matching requests Often as a session identifier inside a cookie By whatever transport the application chooses; the format does not require a cookie
Expiry and ending access Expiry attributes control browser retention, not the validity of the application credential The server ends access by removing or invalidating its state; exact behaviour depends on the implementation The token can carry an expiry claim; whether it can be revoked earlier depends on the application, since the format does not define revocation
Main security considerations Attributes such as HttpOnly, Secure and SameSite; automatic sending raises CSRF concerns Protecting the identifier and managing its lifecycle Signing gives integrity, not confidentiality; a token held in the browser still carries browser-side risks

How the three combine

The confusion usually comes from seeing the layers used together. The three patterns below cover most real designs.

Cookie carrying a session identifier

This is the classic server-managed session:

  1. The user submits a login form over HTTPS.
  2. The server verifies the credentials and creates session state, such as a record containing the user ID and login time.
  3. The server responds with a Set-Cookie header carrying a random, opaque session identifier.
  4. The browser attaches that cookie to later requests in the Cookie header.
  5. The server looks up the identifier, loads the session state, and handles the request. Deleting or invalidating the state on the server ends the session.

Cookie carrying a JWT

Here the cookie is only the transport. The browser stores a JWT in a cookie, and the server verifies the token’s protection on each request. The server does not need to store the session in this design, but whether it keeps any extra state (for example, a list of revoked tokens) is an implementation choice. The cookie’s attributes still matter, because the browser attaches the cookie automatically.

Rank #2
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option

JWT outside a cookie

An application can send the JWT in another header it chooses, such as an Authorization header, which the page’s script adds to requests. The browser then does not attach the token automatically, so the automatic-sending concern that applies to cookies does not apply in the same way. The trade-off moves elsewhere: the script that adds the token can read it, so where and how the token is stored becomes the main question.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Browser cookie security

Cookie security is set by attributes on the Set-Cookie header and by how the application uses them. MDN’s guidance on session management recommends cookies for browser session management where possible, largely because the HttpOnly attribute keeps the value away from JavaScript. That is guidance for browser-based clients, not a rule for every architecture.

HttpOnly

HttpOnly prevents JavaScript running in the page from reading the cookie, for example through document.cookie. It limits access through non-HTTP APIs; it does not stop the browser from sending the cookie with requests.

Secure

Secure restricts transmission of the cookie to secure connections (HTTPS). It protects the cookie in transit and is independent of HttpOnly: a cookie can have one attribute without the other.

Rank #4
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers

SameSite

SameSite controls whether the browser attaches the cookie to requests that originate from other sites. It is one of the main tools for reducing cross-site request forgery, but it is a browser-side control and should not be the only defence an application relies on.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

CSRF and ambient authority

Because browsers attach cookies automatically, a request started by a page on another site can carry the user’s credentials. That is the basis of cross-site request forgery (CSRF). HttpOnly does not prevent CSRF, and neither does Secure. Cookie-authenticated applications need separate measures, such as anti-CSRF tokens checked on state-changing requests, together with the SameSite attribute.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Signing and encryption in JWTs

RFC 7519 describes two forms of protection. A signed token (a JSON Web Signature, or a MAC-protected token) lets the receiver verify that the claims were not altered and were issued by a party holding the key. It does not hide the claims: anyone holding the token can decode and read its payload. An encrypted token protects confidentiality so that only the holder of the key can read the claims.

The practical rule is that a signed JWT is integrity-protected, not secret. Personal or sensitive data should not go into a signed-only token unless the application accepts that anyone with the token can read it, and an encrypted form is needed when the claims themselves must stay private.

Choosing a design

Use these questions to decide which combination fits your application:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Does the client run in a browser? If yes, a cookie with HttpOnly, Secure and an appropriate SameSite setting is a well-understood starting point, and it brings CSRF considerations with it.
  • Do you need to end a user’s access immediately? A server-held session makes this a deletion. A self-contained JWT stays valid until its expiry unless the application adds a check against a revocation list or similar state.
  • Must the claims stay private? If so, use an encrypted token form, or keep the data on the server and send only an identifier.
  • Are the clients mixed, such as a browser app plus a mobile app or API consumers? The cookie-based and header-based approaches can coexist, but each needs its own storage and transport rules.

What the sources do not establish

RFC 6265, RFC 7519 and MDN’s documentation define the mechanisms and give cookie-security guidance. They do not establish that JWTs are faster, that they scale better than server-side sessions, or that either approach is universally safer. Performance and scaling claims should be tested against your own workload. This article also gives no cookie-size limit or other numeric benchmark, because the sources cited here do not publish one.

The Bottom Line

Treat cookies, sessions and JWTs as different layers. A cookie is how the browser stores and returns a value; a session is the application state that value points to; a JWT is one format for the value. Choose the combination by your client type, how fast access must end, and whether the claims must stay private, and apply the cookie attributes and CSRF protections whenever the browser carries the credential.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.