davodm / codeigniter4-recaptcha
dev-master
2022-11-22 01:46 UTC
Requires
- google/recaptcha: ^1.2
This package is auto-updated.
Last update: 2024-09-22 06:01:58 UTC
README
这是一个基于 denis303/codeigniter4-recaptcha 的分支仓库,其中有一些拉取请求,所有者/维护者没有接受。所以,我不得不使用这个仓库。
安装
composer require davodm/codeigniter4-recaptcha:dev-master
配置
在 .env 文件中,您需要添加您个人的 ReCaptcha 密钥。
# --------------------------------------------------------------------
# ReCaptcha 2
# --------------------------------------------------------------------
recaptcha2.key = 'XXXXXXXX-XXXXXXXX'
recaptcha2.secret = 'XXXXXXXX-XXXXXXXX'
# --------------------------------------------------------------------
# ReCaptcha 3
# --------------------------------------------------------------------
recaptcha3.key = 'XXXXXXXX-XXXXXXXX'
recaptcha3.secret = 'XXXXXXXX-XXXXXXXX'
recaptcha3.scoreThreshold = 0.5
在 /app/Config/Validation.php 文件中,您需要为验证器添加设置
public $ruleSets = [
...
\Denis303\ReCaptcha\Validation\ReCaptchaRules::class
];
渲染 ReCaptcha v2
helper(['form', 'reCaptcha']);
echo form_open('/form_processing_path', array('id' => 'contactForm'));
echo reCaptcha2('reCaptcha2', ['id' => 'recaptcha_v2'], ['theme' => 'dark']);
echo form_submit('submit', 'Submit');
echo form_close();
渲染 ReCaptcha v3
helper(['form', 'reCaptcha']);
form_open('/form_processing_path', array('id' => 'contactForm'));
echo reCaptcha3('reCaptcha3', ['id' => 'recaptcha_v3'], ['action' => 'contactForm']);
echo form_submit('submit', 'Submit');
echo form_close();
在模型中检查 ReCaptcha
public $validationRules = [
'reCaptcha2' => 'required|reCaptcha2[]'
'reCaptcha3' => 'required|reCaptcha3[contactForm,0.9]'
....
];
在 reCaptcha3 验证器的设置中,您指定的第一个参数是 expectedAction。
所以,如果您想防止 captcha 过期,表单的 id 属性需要与动作名称相同。这允许在表单提交时调用 grecaptcha.execute
以防止 token 过期警告。否则,它可能在没有动作名称的情况下正常工作。
您可以在第二个 reCaptcha3 规则参数中覆盖全局 scoreThreshold 参数。
请注意,在您的表单中不应有任何 submit
字段名称,例如按钮名称。