Sessions and Cookies
100%

Web Security · Lesson 5

Sessions and Cookies

Learn how a cookie connects browser requests to a web session.

A browser often sends another request after a successful login. A session lets the application associate those requests with the signed-in account. One common approach uses a short identifier stored in a browser cookie.

Following a Cookie

These examples are abridged. The response sends a cookie named session using a fake token for illustration.

Set-Cookie: session=sample-token; Secure; HttpOnly; SameSite=Lax

A browser can then include the cookie in a later request that matches its rules.

Cookie: session=sample-token

The server uses the token to look up the session it previously created.

Which header carries the cookie value back in a matching request?

Reading the Attributes

Secure restricts ordinary transmission of the cookie to HTTPS. HttpOnly tells browser page scripts that they cannot read it through the usual cookie API. SameSite controls when the browser includes it in requests initiated from another site. Each setting applies to a different part of browser behavior.

Which attribute tells the browser to restrict transmission of this cookie to HTTPS?

Ending a Session

When a user signs out, the service can invalidate the session on the server and ask the browser to remove its stored cookie. Server-side invalidation matters because a browser may still briefly have an older value. Treat a real session token like a credential and leave it out of screenshots and shared logs.

What should happen when an invalidated session token reaches the server again?

Lesson complete

You finished Sessions and Cookies

You can now follow a session cookie between a browser and a service.

  • Identify the header that sets a cookie and the header that sends it back.

  • Explain the purpose of common cookie attributes.

  • Describe how server-side invalidation ends a session.

Keep your learning progress

Create a free account to save this lesson and continue learning on any device.

Create a free account
Next Lesson
Back to Web Security