CSRF (Cross-Site Request Forgery) stateless tokens.
Stateless CSRF-token generation and verification., (*1)
Instalation
composer require crodas/csrf-token:"^1.0"
Properties
- Hashes are unique per IP
- They require a site secret, so hashes are impossible to forge.
- Hashes expires after a certain amount of time (Default: 1 hour)
How to use it
Initialize the library:, (*2)
require __DIR__ . '/vendor/autoload.php';
CSRF::setSecret($strong_secret_key);
Add it to your forms, (*3)
<input type="hidden" name="_csrf" value="<?php echo CSRF::generate() ?>" />
And then verify the hashes are legit and still valid:, (*4)
if (empty($_POST['_csrf']) || !CSRF::verify($_POST['_csrf'])) {
throw new Exception("CSRF Token is invalid");
}