PHP code example of codecollab / csrf-token

1. Go to this page and download the library: Download codecollab/csrf-token library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

codecollab / csrf-token example snippets




$csrfToken = new \CodeCollab\CsrfToken\Token\Handler($storage, $generator);

$theToken  = $csrfToken->get(); // this will generate a new token if it doesn't exist yet

var_dump($csrfToken->isValid($theToken)); // true
var_dump($csrfToken->isValid('invalid token')); // false



$csrfToken = new \CodeCollab\CsrfToken\Token\Handler($storage, $generator);

$theToken  = $csrfToken->get(); // this will generate a new token if it doesn't exist yet

var_dump($csrfToken->isValid($theToken)); // true
var_dump($csrfToken->isValid('invalid token')); // false

$csrfToken->generate();

var_dump($csrfToken->isValid($theToken)); // false

 declare(strict_types=1);

use CodeCollab\CsrfToken\Storage\Storage;

class Session implements Storage
{
    public function exists(string $key): bool
    {
        return array_key_exists($key, $_SESSION);
    }

    public function get(string $key): string
    {
        return $_SESSION[$key];
    }

    public function set(string $key, string $token)
    {
        $_SESSION[$key] = $token;
    }
}