Authentication

Use PHP Sessions Safely

Configure session cookies, regenerate identifiers and protect authenticated PHP sessions.

Overview

PHP sessions let an application associate requests with server-side state. The session identifier is a bearer credential: anyone who obtains it may be able to act as the user. Protect both the cookie and the session lifecycle.

Set cookie parameters before starting

Use HTTPS for the whole authenticated site and configure cookies with Secure, HttpOnly and an appropriate SameSite policy. Set a narrow path and avoid unnecessarily broad domain scope. Start the session before output is sent.

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

Only use secure => true when the application is correctly served over HTTPS. Do not weaken cookie protections to compensate for a broken proxy configuration; configure trusted proxy handling correctly.

Regenerate after privilege changes

After login and privilege changes, regenerate the session ID to reduce session fixation risk. Clear authentication state on logout and destroy the server-side session. Consider idle and absolute expiration policies for sensitive applications.

Do not store secrets unnecessarily

Store only the minimum state needed in the session. Never place passwords, payment card data or long-lived API secrets in session variables. Validate authorization on every protected request rather than assuming that possession of a session means every action is allowed.

Official references

PHP session security PHP session configuration

Start sessions with secure cookie settings

Set cookie parameters before starting the session. Use HTTPS in production and enable HttpOnly and Secure; choose SameSite based on the application's cross-site workflow.

<?php
session_set_cookie_params([
    'lifetime' => 0,
    'path' => '/',
    'secure' => true,       // HTTPS production site
    'httponly' => true,
    'samesite' => 'Lax',
]);
session_start();

Regenerate the session identifier after login and privilege changes. Clear session data and expire the cookie during logout. Configure idle and absolute timeouts for sensitive applications.

Treat the session ID as a credential

Never place session identifiers in URLs, analytics events or application logs. Require CSRF protection for state-changing browser requests; SameSite cookies are helpful but do not replace CSRF tokens in every workflow.

Authorization remains separate

A valid session only proves that the browser presents a session identifier. Each protected action must still verify the user's identity, role and permission for the requested resource. Do not rely on hiding a button in the interface.

Operational checklist

  • HTTPS is enforced.
  • Session cookies are Secure, HttpOnly and appropriately SameSite.
  • IDs are regenerated after authentication and privilege changes.
  • Logout invalidates server-side state and expires the cookie.
  • Idle and absolute timeouts are defined.
  • CSRF protection covers state-changing requests.