Session

The Session class in PHPFast provides a structured way to manage user sessions securely. It includes features like:

Basic session handling (start, set, get, delete, destroy). Flash messages (temporary session data that disappears after one request). Security features such as session regeneration to prevent session fixation attacks. Session timeout handling to automatically destroy inactive sessions. CSRF protection with token generation and validation.

Managing Session Variables

set()

Stores a session variable with a given key and value.

Example:

Session::set('user_id', 123); // Stores user ID in session

get()

Retrieves the value of a session variable using its key.

Example:

$user_id = Session::get('user_id'); // Retrieves user ID from session

del()

Completely clears all session data, effectively logging out the user and resetting the session

Example:

Session::del('user_id'); // Deletes the user_id session variable

destroy()

Completely clears all session data, effectively logging out the user and resetting the session.

Example:

has()

Checks if a session variable exists, allowing conditional logic based on whether a specific piece of session data is present.

Example:

Flash Messages

flash()

Storing a Flash Message

Example:

Retrieving a Flash Message

Example:

has_flash()

Checking a Flash Message

Example:

Enhancing Session Security

regenerate()

Generates a new session ID while keeping existing session data intact. This prevents session fixation attacks, where attackers attempt to exploit predefined session IDs. It is commonly used after login to prevent unauthorized access.

Example:

checkSessionTimeout()

Enforces a session timeout based on user inactivity. If the user remains inactive beyond the specified time limit (e.g., 1800 seconds for 30 minutes), the session is invalidated, and the user is logged out automatically.

Example:

CSRF Protection

CSRF tokens protect forms from unauthorized requests.

csrf_token()

Generates a unique CSRF token, which should be included in forms as a hidden field. This ensures that each form submission is validated before being processed.

Example:

csrf_verify()

Checks the submitted CSRF token against the stored token to ensure the request is legitimate. If the verification fails, the request is blocked, protecting against unauthorized form submissions.

Example:

Last updated

Was this helpful?