Staying signed in
A sign-in is followed by many separate requests. A session lets an application connect those requests to the account that authenticated, while continuing to check whether the session and requested access are valid.
Remembering a sign-in
Ava signs in to the photo application and opens an album. Then she opens another album, changes its title, and visits her account settings. The application does not ask for her password on every page. Somehow, it connects these separate requests to the sign-in it accepted a moment ago.
That continuity is a session. HTTPThe Hypertext Transfer Protocol, the rules browsers, apps, and servers use to send requests and responses on the web. does not remember Ava by itself. The application needs a way to recognize a later request, find the account it belongs to, and decide whether to keep accepting it.
A session in practice
In one common design, the server creates a session record after sign-in. It records the account and the session's lifetime, then gives the browser a randomly generated, unpredictable identifier. The identifier points to the record; it does not need to contain Ava's password or profile.
The identifier travels in a cookie: a small named value that a server asks the browser to keep, using a Set-Cookie header in a response. On later requests to the same place, the browser sends the cookie back by itself in a Cookie header. The server looks up the session and finds Ava's account. That explains how opening a second album can work without another password prompt.
Using and protecting a session
Recognizing Ava's session is only the beginning of a request. The server must also check that the session remains valid and that Ava can perform the action she requested. Signing in once does not give her access to every album.
There is another problem at the moment of sign-in. Many applications start a session before anyone signs in, to remember a language choice or what is in a basket. Suppose an attacker starts one of these anonymous sessions, notes its identifier, and gets Ava's browser to use it. A page on a name under photos.example that the attacker controls could do that by setting a cookie for the whole domain. If the server keeps that identifier when Ava signs in, the attacker may be able to use the same value to act as Ava. This is session fixation.
To prevent that handover, the application issues a new, unpredictable identifier when authenticationChecking evidence that someone, or some application, controls an account, such as a password or a passkey. It establishes which account is being used, not what that account may do. succeeds and stops accepting the old one. Replacing the cookie in Ava's browser is only half the job; the server must reject the previous value too.
For a sensitive change, such as replacing a password, the application may ask Ava to authenticate again. That produces fresh evidence of control. Merely changing a session identifier does not.
Optional detail: replacing the identifier at sign-in
These readable values are placeholders; real session identifiers must be randomly generated and unpredictable. Before sign-in, the browser sends:
Cookie: __Host-session=OLD_ANONYMOUS_IDAfter verifying the credentials, the server creates a new session associated with the account, invalidates the old identifier, and replaces the browser's cookie:
Set-Cookie: __Host-session=NEW_AUTHENTICATED_ID; Path=/; Secure; HttpOnly; SameSite=Lax
Subsequent requests use NEW_AUTHENTICATED_ID, and
OLD_ANONYMOUS_ID no longer grants access. Most web
frameworks, the libraries applications are built with, include session management that can make this change;
check that yours does.
Ending a session
Later, Ava walks away from her computer. An idle timeout can end her session after a period without activity. An absolute timeout limits the total session lifetime even if she keeps using it. A service chooses these limits according to what it protects and may renew sessions under its own rules.
When Ava chooses Sign out, the server-stored design we followed needs to invalidate the session record and remove the browser's cookie. Otherwise, a copied cookie could still point to a session the server accepts.
Closing a browser is less definite. A browser may restore its cookies when it reopens, and an old page may remain visible after a session ends. The useful question is whether the server accepts the next protected request.
So far, one application has managed this continuity on its own. Once sign-in happens at a separate service, that service keeps a session too, and the two sessions can end at different times.
Check your understanding
Try these questions before moving on. If an answer isn't right, use the feedback and try again.
Try it in the Lab
- Follow Ava's session until it endsMove between pages after one sign-in, then end the session from Ava's account and from the administrator's view.