Skip to content
What is OpenID Connect?
What is OAuth 2.0?
×

How do websites remember you?

You log in once, and the site knows you on every page. That's a session. Here's how to keep it safe.

Tap the right side to continue →

A session ID is your wristband.

The web forgets you after every request. So after login, the site gives your browser a session ID. Your browser shows it with each request, like a wristband at a concert.

Make it impossible to guess.

If IDs count up, an attacker can just try the next one. Use a secure random generator, with at least 64 bits of randomness. In hex, that's 16 or more characters.

Keep the ID meaningless.

Never put names, roles, or personal data inside the ID. Keep that on the server. And rename default cookies like PHPSESSID, so you don't advertise your tech stack.

Use cookies, not the URL.

An ID in the web address leaks into browser history, server logs, and links to other sites. Cookies keep it out of sight. Accept session IDs only from cookies.

Lock the cookie with three flags.

Secure sends it only over HTTPS. HttpOnly stops page scripts from reading it. SameSite keeps other sites from sending it along.

Don't keep tokens in local storage.

Any script on your page can read local storage. One cross-site scripting bug, and every token is gone. An HttpOnly cookie stays out of reach.

Watch out for session fixation.

An attacker tricks you into using an ID they already know. You log in, and now their ID is logged in too. The fix: give a brand new ID at login, and destroy the old one. Do the same after a password change or role upgrade.

Sessions must expire.

An idle timeout ends a session after inactivity: about 2 to 5 minutes for high-value apps, and 15 to 30 for low-risk ones. An absolute timeout ends it no matter what, like after a workday. Enforce both on the server.

Log out for real.

Deleting the cookie isn't enough. A stolen copy would still work. On logout, end the session on the server, then clear the cookie. Put a logout button on every page.

HTTPS for the whole visit.

Use HTTPS from the first page to the last, not just at login. Turn on HSTS, so browsers never fall back to plain HTTP.

Watch for strange behavior.

Lots of wrong IDs from one address? Someone is guessing. A session suddenly jumping to a new network or browser? Flag it. Log sessions with a hashed ID, never the real one.

Quick check

Tap your answer.

Where should a session ID travel?

A user logs in. What happens to their session ID?

On logout, is deleting the cookie enough?

Let's recap.

Random, meaningless, in a cookie.

Locked with Secure, HttpOnly, and SameSite.

New ID at login. Expire it on the server.

Real logout, and HTTPS all the way.

Users stay logged in. Attackers stay out.

Next short →

Source: OWASP Session Management Cheat Sheet.

How should apps store passwords?
What is MFA?
Authn vs. Authz
×