v1.0.0.0
StocketBase
- <?php
-
- declare(strict_types=1);
-
- final class Csrf
- {
- public static function token(): string
- {
- if (empty($_SESSION['csrf_token'])) {
- $_SESSION['csrf_token'] = bin2hex(random_bytes(32));
- }
-
- return $_SESSION['csrf_token'];
- }
-
- public static function field(): string
- {
- return '<input type="hidden" name="csrf_token" value="' . htmlspecialchars(self::token()) . '">';
- }
-
- public static function verify(?string $token): bool
- {
- return is_string($token) && isset($_SESSION['csrf_token']) && hash_equals($_SESSION['csrf_token'], $token);
- }
- }
-