csrf.php
Table of Contents
Functions
- generateCsrfToken() : string
- Generate a CSRF token and store it in session
- validateCsrfToken() : bool
- Validate CSRF token
- csrfField() : string
- Get CSRF token field HTML
Functions
generateCsrfToken()
Generate a CSRF token and store it in session
generateCsrfToken() : string
Generates a cryptographically secure random token (64 hex characters) and stores it in the session along with a timestamp.
Return values
string —The generated token (64 hex characters), or empty string if session is not active
validateCsrfToken()
Validate CSRF token
validateCsrfToken(string $token) : bool
Validates that the provided token matches the token stored in the session and that the token has not expired (1 hour expiration).
Parameters
- $token : string
-
The token to validate
Return values
bool —True if the token is valid and not expired, false otherwise
csrfField()
Get CSRF token field HTML
csrfField() : string
Generates an HTML hidden input field containing the CSRF token. Uses an existing token if available, otherwise generates a new one.
Return values
string —HTML input field with CSRF token, or empty string if session is not active