los/losrecaptcha

ZF2 模块,用于与谷歌的新 ReCaptcha 服务表单集成

2.2.0 2020-01-02 13:26 UTC

This package is auto-updated.

Last update: 2024-08-29 04:13:06 UTC


README

PHP 模块,用于使用谷歌的 ReCaptcha v2 系统

https://www.google.com/recaptcha/intro/index.html

Zend 表单

要使用与 Zend\Form,只需像默认 ReCaptcha 元素一样初始化

$this->add([
   'name' => 'captcha',
    'type' => 'captcha',
    'options' => [
        'captcha' => new LosReCaptcha\Captcha\ReCaptcha([
            'site_key' => $siteKey,
            'secret_key' => $siteSecret,
        ]),
    ],
]);

对于 不可见 ReCaptcha

// ...
$this->add([
   'name' => 'captcha',
    'type' => 'captcha',
    'options' => [
        'captcha' => new \LosReCaptcha\Captcha\Invisible([
            'site_key' => $siteKey,
            'secret_key' => $siteSecret,
            'callback' => 'captchaSubmit', // Callback to submit the form
            'button_id' => 'submit-button', // Button id to submit the form
        ]),
    ],
]);
// ...
$this->add([
    'name' => 'submit-button',
    'type' => \Zend\Form\Element\Button::class,
    'options' => [
        'label' => _('Log In'),
    ],
    'attributes' => [
        'id'    => 'submit-button',
        'class' => 'btn btn-block btn-primary',
        'value' => _('Log In'),
    ],
]);

在不可见 ReCaptcha 的视图中

function captchaSubmit() {
  // Any js code, eg. fields validation
  document.getElementById("login").submit();
}

对于 Zend Expressive,你可以在 config/config.php 中通过 ConfigProvider 注入配置

<?php
// ...
$aggregator = new ConfigAggregator([
    // ...
    \LosReCaptcha\ConfigProvider::class,
    // ...
], $cacheConfig['config_cache_path']);

return $aggregator->getMergedConfig();