middlewares / recaptcha
使用Google reCAPTCHA进行垃圾邮件防护的中间件
v2.0.1
2020-12-02 00:06 UTC
Requires
- php: ^7.2 || ^8.0
- google/recaptcha: ^1.1
- middlewares/utils: ^3.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.0
- laminas/laminas-diactoros: ^2.3
- oscarotero/php-cs-fixer-config: ^1.0
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^8|^9
- squizlabs/php_codesniffer: ^3.0
README
使用google/recaptcha库进行垃圾邮件防护的中间件。如果请求无效,则返回403
响应。有关Google reCAPTCHA的更多信息。
需求
- PHP >= 7.2
- 一个PSR-7 http库
- 一个PSR-15中间件调度器
安装
此包可通过Composer安装和自动加载,名称为middlewares/recaptcha。
composer require middlewares/recaptcha
示例
$dispatcher = new Dispatcher([ new Middlewares\Recaptcha($secretKey), //in your view function () { echo '<div class="g-recaptcha" data-sitekey="XXX"></div>'; echo '<script type="text/javascript" src="https://www.google.com/recaptcha/api.js"></script>'; } ]); $response = $dispatcher->dispatch(new ServerRequest());
使用方法
您需要一个用于应用的密钥API。您可以在https://www.google.com/recaptcha/admin注册它
$secretKey = 'Your-Secret-Key'; $recaptcha = new Middlewares\Recaptcha($secretKey);
可选地,您可以将Psr\Http\Message\ResponseFactoryInterface
作为第二个参数提供,以创建错误响应(403
)。如果没有定义,则Middleware\Utils\Factory将自动检测。
$responseFactory = new MyOwnResponseFactory(); $recaptcha = new Middlewares\Recaptcha($secretKey, $responseFactory);
ipAttribute
默认使用REMOTE_ADDR
服务器参数来获取客户端IP。此选项允许使用请求属性。与IP检测中间件(例如client-ip)结合使用很有用
$dispatcher = new Dispatcher([ //detect the client ip and save it in "ip" attribute (new Middlewares\ClientIP())->attribute('ip'), //use that attribute (new Middlewares\Recaptcha($secretKey))->ipAttribute('ip') ]);
有关最新更改的更多信息,请参阅CHANGELOG,有关贡献细节,请参阅CONTRIBUTING。
MIT许可证(MIT)。有关更多信息,请参阅LICENSE。