1. Go to this page and download the library: Download rafalmasiarek/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/ */
use rafalmasiarek\Csrf\ClientContextProviderInterface;
class SlimRequestProvider implements ClientContextProviderInterface {
public function __construct(private \Psr\Http\Message\ServerRequestInterface $request) {}
public function getIp(): string {
return $this->request->getAttribute('client_ip') ?? '';
}
public function getUserAgent(): string {
return $this->request->getHeaderLine('User-Agent');
}
}
$csrf = new Csrf($key, 900, new SlimRequestProvider($request));
$csrf = (new Csrf($masterKey32Bytes, 900))
->withContainer('transfer-funds', [
'bind_ip' => false,
'bind_ua' => true,
'origin' => 'https://app.example.com', // trusted, static — never from a request header
]);
$token = $csrf->generateFor('transfer-funds');
// throws \RuntimeException at generation time if 'origin' is set but does not
// normalize to a valid http(s) origin (fail loud on misconfiguration)
$ok = $csrf->validateFor(
'transfer-funds',
$_POST['_csrf'] ?? null,
null,
null,
$_SERVER['HTTP_ORIGIN'] ?? null // or omit — resolved automatically via OriginProviderInterface
);
// The app issuing tokens — still has its own master key for every other container.
$csrf = (new Csrf($masterKey32Bytes, 900))
->withContainer('side-app', ['private_key' => $sideAppKey32Bytes]);
// A separately deployed consumer — only ever holds $sideAppKey32Bytes, never $masterKey32Bytes.
$sideCsrf = (new Csrf($anyUnrelated32ByteKey, 900))
->withContainer('side-app', ['private_key' => $sideAppKey32Bytes]);
$token = $csrf->generateFor('side-app');
$sideCsrf->validateFor('side-app', $token); // true — the two instances share only this one key
$csrf = (new Csrf($masterKey32Bytes, 900))
->withContainer('transfer-funds', ['f from another:
$pair = $csrf->issueFor('transfer-funds');
// <input type="hidden" name="_csrf" value="<?= $pair->token
use rafalmasiarek\Csrf\SessionBindingProviderInterface;
final class JwtSessionBindingProvider implements SessionBindingProviderInterface
{
public function __construct(private string $jwtSessionClaim) {}
public function getSessionBinding(): string
{
return $this->jwtSessionClaim; // must change on login/logout/privilege elevation
}
}
$csrf = new Csrf($masterKey32Bytes, 900, null, new JwtSessionBindingProvider($claim));
use League\Plates\Engine;
use rafalmasiarek\Csrf\Csrf;
use rafalmasiarek\Csrf\Helpers\Plates\CsrfExtension;
$view = new Engine(__DIR__.'/views');
$csrf = new Csrf($masterKey, 900);
CsrfExtension::register($view, $csrf);
// In template: <?= csrf_field('signup')
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
use rafalmasiarek\Csrf\Csrf;
use rafalmasiarek\Csrf\Helpers\Twig\CsrfExtension;
$twig = new Environment(new FilesystemLoader(__DIR__.'/views'));
$csrf = new Csrf($masterKey, 900);
$twig->addExtension(new CsrfExtension($csrf));
// In template: {{ csrf_field('signup')|raw }}
use Illuminate\View\Compilers\BladeCompiler;
use rafalmasiarek\Csrf\Csrf;
use rafalmasiarek\Csrf\Helpers\Blade\CsrfBlade;
// In a service provider boot():
CsrfBlade::register($this->app->make(BladeCompiler::class), app(Csrf::class));
// In Blade: @csrfField('signup')
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.