thelia / re-captcha-module
此模块允许您轻松地将reCAPTCHA添加到您的表单中
3.0.1
2023-01-06 13:44 UTC
Requires
- thelia/installer: ~1.1
README
此模块允许您轻松地将reCAPTCHA添加到您的表单中
安装
Composer
在主thelia composer.json文件中添加它
composer require thelia/re-captcha-module:~2.0.0
用法
在使用此模块之前,您必须在此处创建Google API密钥 http://www.google.com/recaptcha/admin
然后在此处配置reCAPTCHA访问 http://your_site.com/admin/module/ReCaptcha
使用在Google页面上获得的密钥,并选择您想要的captcha样式
然后您需要开发者的帮助来在模板中添加一些钩子并分发检查事件,请参见以下详细信息。
钩子
首先,如果您在模板中没有 {hook name="main.head-top"}
钩子,您必须在head顶部放置此钩子 {hook name="recaptcha.js"}
然后在您想要检查用户是否为人类的每个表单中添加此钩子 {hook name="recaptcha.check"}
注意:如果您想使用不可见captcha,此钩子必须直接放置在表单标签中,如下所示
<form id="form-contact" action="{url path="/contact"}" method="post">
{hook name="recaptcha.check"}
// End of the form
</form>
事件
为了在服务器端检查captcha是否有效,您必须分发 "CHECK_CAPTCHA_EVENT",如下所示
$checkCaptchaEvent = new ReCaptchaCheckEvent();
$eventDispatcher->dispatch($checkCaptchaEvent, ReCaptchaEvents::CHECK_CAPTCHA_EVENT);
然后检查结果在 $checkCaptchaEvent->isHuman()
中可用,作为布尔值,因此您可以进行如下测试
if ($checkCaptchaEvent->isHuman() == false) {
throw new \Exception('Invalid captcha');
}
不要忘记在类的顶部添加此use
use ReCaptcha\Event\ReCaptchaCheckEvent;
use ReCaptcha\Event\ReCaptchaEvents;