szmnmichalowski / zf2-nocaptcha
Zend Framework 2 的无 CAPTCHA reCAPTCHA 模块
1.0.0
2017-03-25 22:33 UTC
Requires
- php: ^5.6 || ^7.0
- zendframework/zend-captcha: 2.7.1
Requires (Dev)
- zendframework/zend-config: 2.6.0
- zendframework/zend-test: ^2.5
This package is not auto-updated.
Last update: 2024-09-20 21:35:45 UTC
README
NoCaptcha 是一个与 Google reCAPTCHA 新版本集成的 Zend Framework 2 模块。
有关 "无 CAPTCHA reCAPTCHA" 的更多信息
安装
您可以通过将此项目克隆到您的 ./vendor/ 目录或使用 composer(推荐)来安装此模块
1. 将此项目添加到您的 composer.json
"require": {
"szmnmichalowski/zf2-nocaptcha": "dev-master"
}
2. 更新您的依赖项
$ php composer.phar update
3. 将模块添加到您的 application.config.php
return array(
'modules' => array(
'Application',
'NoCaptcha' // <- Add this line
)
);
使用方法
1. 在 layout.phtml 的 head 部分
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
2. 在 https://www.google.com/recaptcha/admin#createsite 上注册您的站点。
注册您的站点后,您将获得
- 站点密钥
- 密钥
没有这两个密钥,模块无法正常工作
3. 将这些密钥传递给 \NoCaptcha\Captcha\ReCaptcha 类
示例 #1: 基本示例
$options = array(
'site_key' => 'YOUR_SITE_KEY',
'secret_key' => 'YOUR_SECRET_KEY',
);
$captcha = new \NoCaptcha\Captcha\ReCaptcha($options);
在您的表单类中
...
$this->add(array(
'type' => 'Zend\Form\Element\Captcha',
'name' => 'captcha',
'attributes' => array(
'id' => 'recaptcha-response',
),
'options' => array(
'label' => 'Are you a bot?',
'captcha' => $captcha // <-- Object of NoCaptcha\Captcha\ReCaptcha
)
));
...
最后一步是在视图中渲染验证码输入
<div class="form-group">
<?php echo $this->formlabel($form->get('captcha')); ?>
<div>
<?php echo $this->formCaptcha($form->get('captcha')); ?>
<div class="error-message">
<?php echo $this->formElementErrors($form->get('captcha')); ?>
</div>
</div>
</div>
示例 #2: 高级示例
$options = array(
'site_key' => 'YOUR_SITE_KEY',
'secret_key' => 'YOUR_SECRET_KEY',
'theme' => 'dark',
'type' => 'image',
'size' => 'normal',
'messages' => array(
'errCaptcha' => 'Custom message when google API return false'
),
'service_options' => array(
'adapter' => 'Zend\Http\Client\Adapter\Curl', // override default HttpClient adapter options
)
);
$captcha = new \NoCaptcha\Captcha\Recaptcha($options);
或者您可以使用设置器
$captcha->setSiteKey($siteKey);
$captcha->setSecretKey($secretKey);
$captcha->setTheme('dark');
$captcha->setType('image');
$captcha->setSize('normal');
NoCaptcha 使用 Zend\Http\Client 来验证验证码是否有效。如果出于某些原因您想向 Client 类传递额外的设置,您可以这样做
$service = $captcha->getService();
$service->setOptions(array(
'sslverifypeer' => false
));
reCAPTCHA 选项
主题
- light(默认值)
- dark
类型
- image(默认值)
- audio
大小
- normal(默认值)
- compact
有关选项的更多信息,请参阅 https://developers.google.com/recaptcha/docs/display