Authentication

Protect PHP Applications Against Session Fixation

Regenerate session identifiers at trust changes and configure cookies and session storage to reduce session theft risk.

Session fixation occurs when an attacker causes a victim to use a session identifier known to the attacker. If the application keeps that identifier after authentication or privilege changes, the attacker may be able to reuse it.

Regenerate identifiers at authentication boundaries

After successful login, regenerate the session identifier and ensure the old identifier is invalidated. Apply the same principle after privilege elevation or other important trust changes. Do not regenerate on every request without understanding the effects on concurrent tabs and requests.

Configure session cookies

Use Secure on HTTPS sites, HttpOnly to prevent direct JavaScript access and an appropriate SameSite policy. Set cookie scope narrowly and avoid unnecessarily broad domain attributes. Ensure the application does not accept session identifiers from URL parameters.

Protect session state

Store only what is needed in the session. Enforce idle and absolute expiration policies appropriate to the sensitivity of the application. Clear session state and invalidate the server-side session during logout.

Test the lifecycle

Test login, logout, password changes, privilege changes and session expiry. Verify that a pre-login session identifier cannot continue as an authenticated session after login.

Checklist

  • Regenerate the session ID after login.
  • Regenerate after privilege changes where appropriate.
  • Set Secure, HttpOnly and SameSite cookie attributes.
  • Enforce idle and absolute expiration.
  • Invalidate server-side state on logout.
Share this page Help other developers discover this resource.