middlewares/recaptcha

使用Google reCAPTCHA进行垃圾邮件防护的中间件

v2.0.1 2020-12-02 00:06 UTC

This package is auto-updated.

Last update: 2024-08-29 05:00:08 UTC


README

Latest Version on Packagist Software License Testing Total Downloads

使用google/recaptcha库进行垃圾邮件防护的中间件。如果请求无效,则返回403响应。有关Google reCAPTCHA的更多信息。

需求

安装

此包可通过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