daycry/recaptcha

CodeIgniter 4 的 Recaptcha 库

v1.0.0 2022-03-01 16:42 UTC

This package is auto-updated.

Last update: 2024-08-29 05:44:34 UTC


README

Donate

Recaptcha 库

Codeigniter 4 的 ReCaptcha

通过 composer 安装

使用 composer install 命令使用此包

> composer require daycry/recaptcha

手动安装

下载此存储库,然后通过编辑 app/Config/Autoload.php 并将 Daycry\ReCaptcha 命名空间添加到 $psr4 数组中来启用它。例如,如果您将其复制到 app/ThirdParty

$psr4 = [
    'Config'      => APPPATH . 'Config',
    APP_NAMESPACE => APPPATH,
    'App'         => APPPATH,
    'Daycry\ReCaptcha' => APPPATH .'ThirdParty/recaptcha/src',
];

设置

运行命令

> php spark recaptcha:publish

此命令将复制一个配置文件到您的应用命名空间。

配置

在 .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 = [
    ...
    \Daycry\ReCaptcha\Validation\ReCaptchaRules::class
];

渲染 ReCaptcha v2

helper(['form', 'reCaptcha']);

echo form_open();

echo reCaptcha2('reCaptcha2', ['id' => 'recaptcha_v2'], ['theme' => 'dark']);

echo form_submit('submit', 'Submit');

echo form_close();

渲染 ReCaptcha v3

helper(['form', 'reCaptcha']);

echo form_open();

echo reCaptcha3('reCaptcha3', ['id' => 'recaptcha_v3'], ['action' => 'contactForm']);

echo form_submit('submit', 'Submit');

echo form_close();

如果您正在使用 Twig: https://github.com/daycry/twig

在控制器中的助手数组中添加助手

helper(['form', 'reCaptcha']);

在配置文件中的 functions_safe 中添加 'reCaptcha3' 并调用助手函数。

public $functions_safe = [ 'form_hidden', 'form_open', 'form_close', 'csrf_token', 'csrf_hash', 'url_title', 'reCaptcha3' ];

并在 twig 文件中调用该函数。

{{ reCaptcha3( 'reCaptcha', {'id' : 'recaptcha_v3'}, {'action' : 'signup'} ) | raw }}

在模型中检查 ReCaptcha

public $validationRules = [
    'reCaptcha2' => 'required|reCaptcha2[]'
    'reCaptcha3' => 'required|reCaptcha3[contactForm,0.9]'
    ....
];