All lessons Leer en español

Foundations · Unit 13 · Lesson 2 of 2

State, cookies, and sessions

Explain how a site remembers a visit without confusing browser storage with identity or permission.

7 minready

Helpful before thisWeb application architectures

After this lesson you can

  • distinguish a cookie, a session, and application state
  • explain the separate purposes and limits of common cookie controls
  • describe session expiry and per-action authorization in a fictional booking app

A fictional theater remembers your sign-in while you move from seat selection to your bookings. HTTP requests do not inherently carry a memory of earlier requests. The application builds continuity using identifiers and stored state. That continuity is useful, but it creates a lifecycle that must be managed deliberately.

State, cookies, and sessionsThe browser sends an eligible cookie; the service checks the session; each requested action still needs its own permission decision.Browser cookieStored value and send rulesSession validationIdentity and expiryAction authorizationThis account, this booking
The browser sends an eligible cookie; the service checks the session; each requested action still needs its own permission decision.

A cookie is storage with delivery rules

A cookie is a browser-stored value associated with rules for when it is sent. A server can set one in a response; browser scripts can also create some cookies. Cookies can remember language, preferences, or a session identifier. Their presence does not necessarily mean anyone has signed in.

In one common design, the theater issues an unpredictable identifier after authentication. The browser stores it in a cookie. On later eligible requests, the browser sends the identifier, and the service looks up a session record that connects the visit to an account and expiry policy. The identifier should be treated as a credential even though it is not the person’s password.

The session is a relationship, not a magic file

A coat-check ticket is a useful analogy: its number connects the holder to a stored record. The limit is that a digital identifier can be copied, and the service may apply additional checks. Some applications use signed tokens instead of an opaque identifier lookup. A signed value provides integrity under its verification rules; it is not automatically encrypted, current, or easy to revoke.

Application state includes more than the session. A shopping cart, seat reservation, and payment result may live in separate records with different lifetimes. Expiring a session does not necessarily delete a completed booking, and a cookie’s expiry is not the same as the service’s decision to end a session.

PredictThe theater accepts Maya’s current session. Does that mean she may cancel every booking in the database?

No. The session establishes an authenticated context. The service still needs to authorize the specific action on the specific booking. Identity, session validity, and object permissions are separate questions.

Several controls protect different boundaries

Secure restricts cookie delivery to secure connections in ordinary web deployment. HttpOnly prevents access through script cookie APIs; it does not stop the browser attaching the cookie to eligible requests. SameSite limits delivery in certain cross-site contexts, with behavior depending on its setting and the request. None of these attributes alone supplies every protection an application needs.

Domain and path settings also affect cookie delivery. A path is not a robust security boundary between mutually untrusted applications. Keep scope narrow, protect the channel, and let the server enforce access decisions.

End the relationship clearly

For a server-side session, logout should invalidate the session as well as clear the browser value. Idle and absolute expiration address different durations. After a sensitive identity change, rotating the identifier can avoid carrying an older session relationship forward. User-facing messages should explain when authentication is needed again without claiming that all saved account data has disappeared.

Check yourself

No timer. No penalties. Read the explanation and try again whenever you like.

  1. A cookie remembers the site’s language. Is it necessarily a login session?

    Show the answer

    Correct answer: No; cookie purpose depends on how the application uses it. A language preference does not by itself establish an authenticated identity.

  2. A session cookie is HttpOnly. What does that control?

    Show the answer

    Correct answer: Script access through browser cookie APIs. HttpOnly restricts that access; it does not replace application authorization.

  3. The booking app accepts a current session. Can it skip checking booking ownership?

    Show the answer

    Correct answer: No; the requested action and booking still need authorization. A valid session identifies a context, while the business rule governs access.

  4. Logout only removes a session cookie from this browser. Which conclusion is justified?

    Show the answer

    Correct answer: The server might still accept the old identifier; its session state needs a separate check. Ending local storage and invalidating a server-side session are separate operations.

Try it

  • WriteDraw a fictional coat-check ticket, a browser cookie, and a server session record. Mark which values identify a continuing visit and which component decides whether the visitor may cancel booking A. Then show what logout must change in your model.
References