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(): boolTimeout Management
Session Data Management
Flash Data
Configuration Options
The configure() method accepts the following options:
Basic Example
Timeout Example
Security Best Practices
Always configure before starting session: Call
Session::configure()beforesession_start()Use HTTPS in production: Set
cookie_secure => truewhen using HTTPSRegenerate on privilege escalation: Call
Session::regenerate()after loginImplement timeouts: Use idle and absolute timeouts to limit session lifetime
Use SameSite cookies: Set
cookie_samesiteto 'Lax' or 'Strict' for CSRF protection
Last updated