CIE IGCSE | 5.1.6 Cookies and Sessions

Lesson objective

Explain cookies and their uses, distinguish session and persistent cookies, and describe how cookies can help maintain a login or shopping session.

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

  1. The browser requests a page.

    The server receives the request.

  2. The server returns a response and a cookie.

    For example, it asks the browser to store an opaque identifier such as cart_id = k7x2.

  3. The browser stores the cookie.

    Storage and later use depend on the cookie rules and browser settings.

  4. A later matching request includes the cookie.

    The browser sends the identifier back to the relevant service.

  5. 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

Classify by lifetime, not only by what the cookie is used for
PointSession cookiePersistent cookie
LifetimeNormally the browser session in the teaching modelA specified expiry or lifetime
Later browser sessionNot intended for long-term retention; restoration behaviour can varyCan remain available until expiry or removal
Typical purposeTemporary continuity during a visitRemembering information across visits
Important limitNot identical to the server’s login-session lifetimeNot 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

The cookie can contain data or point to data held by the server
Use from the specificationHow cookies can help
Saving personal detailsRetain a suitable value or identifier linked to details
Tracking user preferencesRemember choices such as language or associate visits with preference data
Holding items in a shopping cartStore small cart information or an identifier linked to the server’s cart
Storing login detailsSupport 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.

MATCH THE COOKIE TERM

Terminology

Terminology

Cookie

Small site-related data stored by the browser and sent with suitable matching requests.

Session cookie

A cookie normally retained for the browser session, without a specified persistent lifetime.

Persistent cookie

A cookie with an expiry or lifetime that can remain across browser sessions.

Expiry

The point or lifetime after which a cookie or session is no longer intended to remain valid.

Session

Related interactions for which application state is maintained.

Session identifier

A value used to associate requests with session information.

Server-side session

State maintained on the server and linked to related requests.

Stateless HTTP

HTTP does not automatically maintain the application’s full history between requests.

User preference

A chosen setting, such as a language or display option.

Shopping cart

Information about items selected for possible purchase.

Login token

A value used to support authenticated recognition, distinct from a raw password.

Tracking

Linking activity across requests or visits.

First-party cookie

A cookie associated with the site being visited.

Third-party cookie

A cookie associated with another site or service in the browsing context.

Cookie matching

Rules determining whether a cookie is sent with a request.

Logout

Ending authenticated use, typically including invalidation of relevant server-side session state.

Questions

Questions

CHECK YOUR UNDERSTANDING

Select all correct choices. Each exact set earns one point.

1. Which describe a cookie?
2. Which distinguish session and persistent cookies?
3. Which are syllabus uses of cookies?
4. Which login statements are correct?
5. Which lifetime statements are correct?
6. Which statements about cart storage are correct?
7. Which privacy statements are correct?
8. Which matching statements are correct?

EXPLAIN AND COMPARE

Write your answers before revealing each sample.

1. Define a cookie.

2. Compare session and persistent cookies.

3. Describe how a cookie can maintain a shopping cart across page requests.

4. Explain how a cookie can support login without containing the raw password.

5. Explain why deleting a preference cookie can change the next visit.

6. Explain why a cookie being present does not guarantee the login is valid.

7. Evaluate using persistent cookies to personalise a website.

APPLY IT | THREE DIFFERENT MEMORIES

A site remembers a language for later visits, keeps an anonymous cart during a visit and maintains an authenticated session on the server. Suggest how cookies can support each. Explain why the cookie lifetime alone does not determine how long the user remains logged in.

Reveal suggested answer

A persistent preference cookie can retain the language. A cart identifier can link requests to the cart, using a lifetime chosen by the site. A login cookie can identify server-side session information. The server independently checks expiry and validity, so retained cookie data does not guarantee an active login.

Flashcards

Flashcards

Click to flip. Select the ideas you need to revisit.

0 cards selected for revision.

    Selections are kept while this page is open.

    Workbook

    Workbook

    COMING SOON

    The workbook for 5.1.6 Cookies and Sessions is coming soon. Complete the matching task and written questions in your exercise book.