HTTP has no memory. Each request arrives as a stranger, so after you log in the site needs some way to recognise you on the next click. Two designs dominate: the server remembers you (a session), or you carry a signed proof of who you are (a token). Both work. They fail in different ways, and I will argue that for most ordinary web applications the older, boring approach is the better default.

Design one: server-side sessions

After a successful login, the server creates a session record in memory or a database and generates a long random identifier. It sends that identifier to the browser in a cookie. On every later request, the browser sends the cookie back, and the server looks the identifier up to find who you are and what you may do.

The identifier itself carries no meaning. It is only a key. Security rests on two things: it must be unguessable (generated by a cryptographically secure random source, with enough length), and it must stay secret. Anyone who obtains a valid identifier can present it and be treated as you, with no password and no MFA prompt, because those checks already happened. This is session hijacking.

Design two: signed tokens

Here the server issues a token containing claims, such as the user ID, roles and an expiry time, and signs it with a secret or private key. JSON Web Token (JWT) is the best-known format. The server does not store the token. On each request it verifies the signature and reads the claims. This suits situations where many separate services need to trust the same login without sharing a session store, and it is common for mobile apps and APIs.

But a stolen token has the same effect as a stolen session identifier: whoever presents it is you, until it expires.

How identifiers get stolen

Conceptually, the routes are few. Malicious script running in the page (a cross-site scripting flaw) can read anything not protected from scripts. Traffic over unencrypted HTTP can be observed by someone on the network. Malware on the user’s own computer can lift stored cookies, and so can a phishing proxy that sits between the user and the real site and captures the session after the user completes MFA. A shared or public computer left logged in is another. And a design flaw called session fixation lets an attacker plant a known identifier before the victim logs in, which works if the site keeps the same identifier after authentication.

Cookie flags that matter

  • HttpOnly: scripts in the page cannot read the cookie, which blunts theft through injected script.
  • Secure: the browser sends it only over HTTPS.
  • SameSite: limits when the cookie accompanies requests started from other sites, which helps against cross-site request forgery.

Tokens stored in the browser’s local storage are readable by any script on the page, which is why many security practitioners prefer keeping them in HttpOnly cookies when the client is a browser.

The real difference: taking access away

With server-side sessions, ending access is trivial: delete the record and the identifier is dead at once. Logging out truly logs you out, an administrator can terminate every session of a compromised account, and a password change can invalidate old sessions.

With a self-contained token, the server is not checking a list, so a stolen token stays valid until it expires. To get revocation back, teams add short lifetimes (minutes), refresh tokens that can be revoked, or a deny list the server consults, which quietly reintroduces the server-side state the design set out to avoid. Misconfigurations also have a history: accepting tokens whose header says no signature is needed, or confusing signing algorithms, have caused real vulnerabilities in libraries, so use a maintained library and fix the permitted algorithms.

Which side I take

For a conventional website with a login, server-side sessions in a secure cookie are simpler, easier to revoke and harder to misuse. Choose tokens when you have a clear need: many independent services, mobile clients, or federation through standards such as OAuth and OpenID Connect. Then keep access tokens short-lived and treat refresh tokens like passwords.

Whichever you pick, issue a fresh identifier after login and after privilege changes, set sensible idle and absolute timeouts, require re-authentication for sensitive actions such as changing the email address or making a payment, and show users their active sessions with a “sign out everywhere” button. That is why logout is a security feature and not decoration.

Leave a Reply

Trending