Files
gestoria-lp/includes/csrf.php
2026-03-01 23:28:54 +00:00

19 lines
585 B
PHP

<?php
function csrfToken(): string {
if (session_status() === PHP_SESSION_NONE) session_start();
if (empty($_SESSION['csrf_token'])) {
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
return $_SESSION['csrf_token'];
}
function csrfField(): string {
return '<input type="hidden" name="csrf_token" value="' . htmlspecialchars(csrfToken()) . '">';
}
function csrfValidate(): bool {
if (session_status() === PHP_SESSION_NONE) session_start();
$token = $_POST['csrf_token'] ?? '';
return hash_equals($_SESSION['csrf_token'] ?? '', $token);
}