jimmiw / csrf
处理纯系统中的CSRF令牌
2.0.0
2023-05-31 07:29 UTC
Requires
- php: >= 7.0
Requires (Dev)
- phpunit/phpunit: ^10.1
README
此包将使您轻松处理表单中的CSRF令牌,适用于可能不使用花哨框架的系统等。
该想法本身来自这里: https://brightsec.com/blog/csrf-token/
入门
该包可在GitHub和Packagist上获取
安装
要使用该系统,请使用composer要求它
composer require jimmiw/csrf
使用组件
使用组件非常简单,只需构造类并调用generateToken。
use Westsworld\CSRF\Generator;
// you can add a custom session handler, when creating the token handler in the construct method.
$tokenHandler = new Generator();
// the generated token is stored in the session
$token = $tokenHandler->generateToken();
<form method="post">
<input type="hidden" name="<?php echo $token->getKey(); ?>" value="<?php echo $token->getValue(); ?>" />
<input type="hidden" name="token-key" value="<?php echo $token->getKey(); ?>" />
... other form fields here
</form>
当表单提交到您的页面时,只需创建一个新的令牌处理程序并调用validateToken。
$tokenHandler = new Generator();
if (! $tokenHandler->validateToken($_POST['token-key'])) {
exit('token is not valid!');
} else {
// handle the form saving here
}