Secure Session Management: Cookies, SameSite, Rotation and Logout

Implement secure web sessions: HttpOnly, Secure and SameSite cookies, session ID rotation, timeouts, CSRF protection, logout everywhere and storage choices.

Published December 4, 2026

After a user logs in, every subsequent request must prove it comes from that user. Sessions do this, and they are a favourite target: steal a session and you skip the password, MFA and passkey entirely. Getting cookie attributes, rotation, expiry and logout right closes most of the doors attackers use.

The concept

A classic server-side session stores state on the server (memory, Redis, database) and gives the browser an unguessable session ID in a cookie. Cookie attributes do much of the security work: HttpOnly prevents JavaScript from reading the cookie (limiting XSS theft), Secure sends it only over HTTPS, SameSite=Lax or Strict stops it being sent on most cross-site requests (mitigating CSRF), and the __Host- prefix forces Secure, Path=/ and no Domain attribute.

Lifecycle rules matter as much as attributes. Rotate the session ID on login and privilege changes to prevent session fixation. Apply idle and absolute timeouts. On logout, destroy the server-side session, not just the cookie. Offer "log out of all devices" and invalidate sessions on password changes. For state-changing requests, combine SameSite with CSRF tokens or origin checks.

Cookie attributes

AttributeValueProtects against
HttpOnlysetSession theft via XSS
SecuresetInterception over plain HTTP
SameSiteLax (or Strict)Most CSRF attacks
__Host- prefixname prefixCookie injection from subdomains
Max-Age / ExpiresboundedIndefinitely valid sessions
Path=/setScope surprises

Session lifecycle

BrowserServerSession storePOST /login (valid credentials)delete pre-login session, create new idSet-Cookie: __Host-sid=…; HttpOnly; Secure; SameSite=Laxrequests with cookiePOST /logoutdestroy session
Rotate on login; destroy on logout.

Timeouts and re-authentication

ControlTypical value
Idle timeout15-60 minutes (shorter for admin)
Absolute timeout8-24 hours, or days with re-auth for sensitive actions
Re-authenticationRequired for password/email change, payments
Concurrent session listingShow devices; allow revoke

Practical examples

A hardened session cookie header

What the response should look like.

Set-Cookie: __Host-sid=8f2c1a9e...; Path=/; Secure; HttpOnly; SameSite=Lax; Max-Age=28800

Express with express-session and Redis

Regenerate on login, destroy on logout.

app.use(session({
  store: new RedisStore({ client: redis }),
  name: '__Host-sid', secret: process.env.SESSION_SECRET, resave: false, saveUninitialized: false,
  cookie: { httpOnly: true, secure: true, sameSite: 'lax', maxAge: 8 * 3600 * 1000 },
}))
app.post('/login', async (req, res, next) => {
  const user = await authenticate(req.body)
  req.session.regenerate(err => { if (err) return next(err); req.session.userId = user.id; res.sendStatus(204) })
})
app.post('/logout', (req, res) => req.session.destroy(() => res.clearCookie('__Host-sid').sendStatus(204)))

Laravel session settings

config/session.php equivalents via environment.

SESSION_DRIVER=redis
SESSION_LIFETIME=60
SESSION_SECURE_COOKIE=true
SESSION_SAME_SITE=lax
# and call $request->session()->regenerate() after login

Common mistakes

  • Not regenerating the session ID after login (session fixation).
  • Logging out by deleting only the cookie while the server session stays valid.
  • SameSite=None without a real cross-site need.
  • Very long sessions without re-authentication for sensitive actions.

Key takeaways

  • Use HttpOnly, Secure, SameSite cookies, ideally with the __Host- prefix.
  • Rotate session IDs on login and privilege changes.
  • Enforce idle and absolute timeouts; destroy sessions server-side on logout.
  • Add CSRF protection and re-authentication for sensitive actions.

FAQ

Sessions or JWTs in cookies?

Either can be secure in HttpOnly cookies; server-side sessions are easier to revoke and inspect.

Does SameSite remove the need for CSRF tokens?

It blocks most CSRF, but tokens or origin checks still help for older browsers, same-site subdomains and edge cases.

Where should sessions be stored?

A shared store like Redis or the database, so any app instance can validate them and revoke them centrally.

More Security articles