Learn
5.1.6 | COOKIES AND SESSIONS
01 | WHAT IS A COOKIE?
A cookie is a small piece of data stored by the browser for a website. The browser can send it with later matching requests, subject to its rules and settings.
For example, a cookie might hold a preference value or an identifier that lets a server find information about a visit.
A cookie is data, not an executable program. It does not itself run instructions like a virus. Its uses can still affect privacy and security.
The server commonly asks the browser to store a cookie through a response. Some cookies can also be set by permitted client-side code.
02 | WHY A WEBSITE NEEDS TO REMEMBER
HTTP is described as stateless: a request does not automatically carry the application’s complete history of earlier requests.
If you add a book to a shopping cart and then open another page, the server needs a way to connect the new request to the appropriate cart.
A cookie can carry a value that provides that connection. The website can use it to retrieve previously stored information or apply a preference.
Cookies are one way of maintaining continuity; they are not the only storage or identification mechanism a web application can use.
03 | FOLLOW A COOKIE EXCHANGE
- The browser requests a page.
The server receives the request.
- The server returns a response and a cookie.
For example, it asks the browser to store an opaque identifier such as cart_id = k7x2.
- The browser stores the cookie.
Storage and later use depend on the cookie rules and browser settings.
- A later matching request includes the cookie.
The browser sends the identifier back to the relevant service.
- The server uses the value.
It can associate the request with the corresponding cart or session information.
This example uses an invented identifier, not a real authentication token. The server does not need every cart item to be stored inside the cookie.
04 | SESSION COOKIES
A session cookie has no explicit persistent expiry lifetime and is normally retained for the browser session. In the syllabus model, it is removed when the browser session ends.
It can support temporary continuity, such as moving between pages during a visit.
Closing one tab is not necessarily ending the browser session. Other tabs or browser windows may still be open. Some browsers also restore session state after reopening, so real behaviour can depend on settings.
The lifetime of a cookie is not a guarantee of how long a server-side login remains valid. The server can expire or invalidate a session independently.
05 | PERSISTENT COOKIES
A persistent cookie has an expiry time or lifetime. It can remain available across browser sessions until it expires, is removed or is otherwise restricted.
For example, a site can remember a chosen language for a later visit. A “remember me” feature may use a persistent token, depending on the site’s design.
Persistent does not mean permanent. A user can delete the cookie, the browser can remove it, or its lifetime can end.
06 | COMPARE THE TWO TYPES
| Point | Session cookie | Persistent cookie |
|---|---|---|
| Lifetime | Normally the browser session in the teaching model | A specified expiry or lifetime |
| Later browser session | Not intended for long-term retention; restoration behaviour can vary | Can remain available until expiry or removal |
| Typical purpose | Temporary continuity during a visit | Remembering information across visits |
| Important limit | Not identical to the server’s login-session lifetime | Not permanent and not necessarily a login cookie |
A shopping cart or login can be implemented using different cookie lifetimes. Do not decide the type solely because a cookie supports a cart or a login.
07 | THE FOUR SYLLABUS USES
| Use from the specification | How cookies can help |
|---|---|
| Saving personal details | Retain a suitable value or identifier linked to details |
| Tracking user preferences | Remember choices such as language or associate visits with preference data |
| Holding items in a shopping cart | Store small cart information or an identifier linked to the server’s cart |
| Storing login details | Support recognition through a login/session or remembered-login identifier |
These uses describe continuity and recognition. They do not require storing all personal information, cart contents or credentials directly in the cookie.
Important clarification: although the syllabus says “storing login details”, secure systems commonly use a session identifier or token, rather than storing the user’s raw password in the cookie.
08 | A LOGIN SESSION IS NOT JUST A COOKIE
A session is a way of maintaining information about related interactions. A server-side login session can link an identifier to an authenticated user and an expiry time.
BROWSER COOKIE
session_id = invented_token
A reference sent with suitable requests.
SERVER-SIDE SESSION
Identifier → user, permissions, expiry
The server checks whether the session is still valid.
After a successful login, the server can issue the cookie. On a later request, it checks the identifier and retrieves the corresponding session information.
Logging out can invalidate the server-side session. A cookie still being present does not necessarily mean its identifier is accepted.
09 | PREFERENCES AND SHOPPING CARTS
Language preference: a cookie containing a value such as language = en can help the site use the same language on a later visit.
Shopping cart: a cookie can identify a server-side cart. Adding another item changes that cart’s stored contents; the identifier connects later requests to it.
Deleting cookies can make a site forget a preference or lose the connection to an anonymous cart. An account-linked cart may still be recoverable after login, depending on the implementation.
10 | TRACKING AND PRIVACY
A cookie identifier can help associate requests or visits. This can support useful personalisation, but can also support tracking of browsing behaviour.
First-party cookies relate to the site being visited. Third-party cookies involve another site or service in the browsing context and can support cross-site recognition where allowed.
Browser restrictions and settings can limit storage and sending. Do not assume every third-party cookie is accepted, or that blocking cookies stops every possible tracking technique.
For this lesson, recognise the trade-off: remembering choices can improve convenience, while linking activity can reduce privacy.
11 | BENEFITS, LIMITS AND SECURITY
Cookies can reduce repeated input, preserve preferences and maintain continuity. However, clearing or blocking them can change site behaviour, and identifiers used for authentication need careful protection.
A stolen valid login token could allow someone to impersonate its user, subject to the server’s checks. HTTPS helps protect the exchange in transit; it does not prevent every way a token could be misused.
Browsers restrict which requests receive cookies using factors such as domain, path and security attributes. A cookie is not automatically sent to every site on the internet.
These are conceptual explanations of protection and risk, not a complete website-security configuration guide.
12 | COMMON MISTAKES
- A cookie is a program: it is data.
- Persistent means forever: expiry and removal still apply.
- A tab closing always removes session cookies: the browser session can continue.
- A session cookie and a server session are identical: one is browser data; the other maintains application state.
- Login requires storing a raw password in a cookie: an identifier or token can support recognition.
- Every cookie is sent to every site: matching and security rules restrict use.
- Blocking cookies makes tracking impossible: other techniques exist.