Cookie attributes and session lifecycle
A session connects requests
HTTP requests are separate messages. A server can maintain login state by associating an opaque, unpredictable session identifier with server-side data. The browser sends the identifier in a cookie when applicable. The identifier acts like a credential, so do not log it or expose it in URLs. A session is not the same as a user account.
Read a secure cookie example
Consider a fictional HTTPS bookstore using a host-only session cookie. This is a static illustration, not a real credential. Secure limits transmission to secure connections (browser localhost exceptions exist); HttpOnly restricts script access to the cookie, not all authenticated requests. SameSite=Lax limits cross-site cookie sending while allowing certain top-level safe-method navigations.
Set-Cookie: session=DEMO_ONLY; Path=/; Secure; HttpOnly; SameSite=LaxScope and site boundaries
Omitting Domain restricts a cookie to the host that set it. Adding Domain can extend it to subdomains. Path selects request paths for cookie sending but is not a robust isolation boundary. SameSite uses site boundaries, which differ from origins; two HTTPS subdomains of example.com can be same-site yet cross-origin. SameSite=None requires Secure in modern browsers; third-party cookie restrictions may still apply.
Rotate, expire and revoke
Generate a fresh session identifier after login or privilege changes and invalidate the old identifier. Enforce idle and absolute timeouts on the server: activity can extend idle time, not the absolute lifetime. Logout invalidates the server-side session and removes the browser cookie. For sensitive actions, consider reauthentication. Choose timeouts based on risk and usability, not a universal number.
Practice: review a session timeline
A fictional policy uses 10 minutes idle and 60 minutes absolute lifetime. A session created at minute 0 and last used at minute 50 expires at minute 60 even if another request was made at minute 59. A session explicitly revoked at minute 5 must not be accepted at minute 6. Explain why cookie expiration alone cannot enforce these decisions.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Cookie attributes and session lifecycle — knowledge check
Reason about browser and server controls using fictional cases.
Which cookie attribute restricts script access to the cookie value?
What does Secure generally restrict?
What should happen after login?
A revoked session cookie remains on a device. What should the server do?
Which timeout is not extended by ordinary activity?