Session

The Session class provides a static helper API for managing PHP sessions with security best practices, timeout management, and flash data support.

Capabilities

  • Configure session security settings (HttpOnly, Secure, SameSite)

  • Regenerate session IDs to prevent fixation attacks

  • Destroy sessions completely (data, cookie, and file)

  • Check idle and absolute session timeouts

  • Manage session data with get/set/has/remove helpers

  • Flash data for one-time messages

Public API

Session Lifecycle

// Configure session security settings (call before session_start())
Session::configure(array $options = []): void

// Regenerate session ID (prevents session fixation)
Session::regenerate(bool $deleteOldSession = true): bool

// Destroy session completely (clears data, cookie, and file)
Session::destroy(): bool

Timeout Management

Session Data Management

Flash Data

Configuration Options

The configure() method accepts the following options:

Basic Example

Timeout Example

Security Best Practices

  1. Always configure before starting session: Call Session::configure() before session_start()

  2. Use HTTPS in production: Set cookie_secure => true when using HTTPS

  3. Regenerate on privilege escalation: Call Session::regenerate() after login

  4. Implement timeouts: Use idle and absolute timeouts to limit session lifetime

  5. Use SameSite cookies: Set cookie_samesite to 'Lax' or 'Strict' for CSRF protection

Last updated