Authentication

Harden PHP Cookies and Session Cookie Settings

Configure session cookies with secure transport, HttpOnly, SameSite and sensible lifetime and rotation policies.

Session cookies are credentials

A session cookie usually represents an authenticated browser session. If it is stolen, an attacker may be able to act as the user. Cookie settings reduce exposure but do not replace HTTPS, session regeneration or authorization checks.

Set cookie attributes before starting the session

Configure session behavior centrally and before session_start():

session_set_cookie_params([
    'lifetime' => 0,
    'path' => '/',
    'secure' => true,
    'httponly' => true,
    'samesite' => 'Lax',
]);

session_start();

Use secure when the application is served over HTTPS. Choose SameSite based on actual cross-site flows; Strict, Lax and None have different behavior. SameSite=None requires Secure in modern browsers.

Regenerate identifiers

Regenerate the session ID after login and privilege changes. Destroy the previous session state according to the application's session handling policy. Avoid accepting session IDs through URL parameters.

Expire and invalidate sessions

Set idle and absolute lifetime policies on the server, not only through cookie expiration. Provide logout that invalidates server-side session data and expires the cookie using matching path and domain attributes.

Validate proxy configuration

If HTTPS terminates at a trusted reverse proxy, configure the application to understand the original scheme only from trusted proxy headers. Do not trust arbitrary client-supplied forwarding headers to decide whether cookies should be secure.

Checklist

  • Use HTTPS and Secure cookies.
  • Set HttpOnly and an appropriate SameSite policy.
  • Configure cookies before session_start().
  • Regenerate IDs after authentication and privilege changes.
  • Enforce server-side idle and absolute timeouts.
  • Invalidate server-side state on logout.
  • Never put session IDs in URLs.

References

PHP Session Configuration · OWASP Session Management Cheat Sheet

Share this page Help other developers discover this resource.