alexstack/google-recaptcha-to-any-form

在任何自定义表单中显示 Google Recaptcha v2 或 v3 或隐形,具有灵活的设置且不影响您现有的代码。也适用于 SilverStripe 4.x/3.x/2.x & Laravel & Wordpress & 其他 CMS。

1.4.0 2019-09-03 06:02 UTC

This package is auto-updated.

Last update: 2024-09-29 04:49:52 UTC


README

Latest Stable Version License Total Downloads

  • 它可以在任何自定义表单中显示 Google Recaptcha v2 或 v3 或隐形,具有灵活的设置且不影响您现有的代码。也适用于 SilverStripe 4.x/3.x/2.x & Laravel & Wordpress & 其他 CMS。

基本示例代码

  • 在 Form_Field_ID 之后显示 Google Recaptcha v2 或 v3
// For Google recaptcha v2
GoogleRecaptcha::show('SiteKey', 'Form_Field_ID');

// For Google recaptcha v3
GoogleRecaptcha::showV3('SiteKey', 'Form_Field_ID');
  • 在后端 php 中验证它
GoogleRecaptcha::verify('SecretKey');

如何安装

composer require alexstack/google-recaptcha-to-any-form

内容

如何在前端页面显示它?

How to display it on frontend page

  • 为您网站的 Google Recaptcha v2 或 v3 账户设置,并获取网站密钥和密钥
  • 导入 GoogleRecaptcha 类
use GoogleRecaptchaToAnyForm\GoogleRecaptcha;
  • 将以下 php 代码放入您的前端模板/页面中,用于 Google Recaptcha v2
GoogleRecaptcha::show($SiteKey, 'Form_ContactForm_Message', 'no_debug', 'mt-4 mb-1', 'Please tick the reCAPTCHA checkbox first!');

google-recaptcha-v2-default

上述代码的解释

  • '$SiteKey': 您网站的 Google Recaptcha 网站密钥。您可以直接在这里放置网站密钥,或从变量或数据库中获取。
  • 'Form_ContactForm_Message': Form_Field_ID,通过 html 代码。例如 ... <input type="text" id="Form_ContactForm_Message" /> ... 您的 Google Recaptcha 将显示在此表单字段之后。
  • 'no_debug': 如果将 debug 改为不勾选“我不是机器人”复选框,则将继续提交表单,然后您将看到失败消息。
  • 'mt-4 mb-1': Google Recaptcha 区域的额外 css 类名。对于 recaptcha v2:如果您想使用暗色主题而不是亮色主题,请添加 theme-dark。如果您已导入 recaptcha/api.js,请添加 no-api-js。
  • '请先勾选 reCAPTCHA 复选框': 如果最终用户没有勾选复选框,则在前端显示的警告消息。提示:您可以将此值更改为 "v3",它将自动切换到使用 Google Recaptcha v3
  • show() 方法的参数默认值
show($site_key,$after_field_id='Form_ContactForm_Comment', $debug='no_debug', $extra_class="mt-4 mb-4", $please_tick_msg="Please tick the I'm not robot checkbox");

如何使用 Google Recaptcha 无形

  • 首先需要获取 Google Recaptcha 无形网站密钥和密钥。它与 v2 不同。
  • 基于上述解释,将一个字符串 invisible 添加到额外的 css 类中即可。例如:mt-4 invisible mb-1
GoogleRecaptcha::show($SiteKey, 'Form_Field_ID', 'no_debug','mb-2 invisible');

Google Recaptcha v3 的前端示例

GoogleRecaptcha::show($SiteKey, 'Form_Field_ID', 'no_debug','show-inline-badge mt-4 mb-3','v3');
// or
GoogleRecaptcha::showV3($SiteKey, 'Form_Field_ID', 'no_debug');

google-recaptcha-v3-default

  • 我们的 Google Recaptcha v3 将在页面加载 10 秒后或 Form_Field_ID(例如,Form_ContactForm_Message)被点击后自动获取 g-recaptcha-response 值。
  • 'no_debug': 如果将 debug 改为 "debug",则始终将空 g-recaptcha-response 提交到后端。
  • 'show-inline-badge mt-4 mb-3': Google Recaptcha inline-badge 的额外 css 类名。移除 show-inline-badge 将显示一个右下角浮动 Recaptcha 徽章而不是 inline-badge。

google-recaptcha-v3-inline

  • 您需要首先设置Google Recaptcha v3站点密钥和密钥。它与v2不同。

  • 如果您不想使用show()方法,您也可以使用自己的代码以自定义样式显示recaptcha。只需确保表单操作方法为POST,然后您仍然可以在后端脚本中使用下面的verify()方法。

如何在后端脚本中进行验证

  • 导入 GoogleRecaptcha 类
use GoogleRecaptchaToAnyForm\GoogleRecaptcha;
  • 将以下PHP代码放入后端PHP文件中的form-submitted-method()。
GoogleRecaptcha::verify($GoogleRecaptchaSecretKey, 'Google Recaptcha Validation Failed!!');
  • 上述代码的描述
    • '$GoogleRecaptchaSecretKey':您网站的Google Recaptcha密钥。您可以直接在这里放置密钥或变量或从数据库中获取。
    • 'Google Recaptcha Validation Failed':如果验证失败,则显示的JavaScript警告消息。如果您不想有JavaScript警告,可以将其设置为null或false。它将根据Google recaptcha验证结果返回true或false。然后您可以选择显示自己的错误消息。
  • verify()方法参数的默认值
verify($secret_key, $break_msg = null, $recaptcha_score=0.5)
  • 如果您正在使用Google Recaptcha v3,它默认将分数<0.5作为失败的结果。如果您想设置不同的值,可以设置分数。例如。
GoogleRecaptcha::verify($SecretKey, 'Google Recaptcha Validation Failed!!', 0.36);

SilverStripe 4.x/3.x的用法示例

  • 导入 GoogleRecaptcha 类
use GoogleRecaptchaToAnyForm\GoogleRecaptcha;
  • 在控制器中创建一个函数以显示recaptcha。例如。
public function showGoogleRecaptcha()   {
    return GoogleRecaptcha::show($SiteKey, 'Form_ContactForm_Message', 'no_debug', 'mt-4 mb-1', 'Please tick the reCAPTCHA checkbox first!');
}
  • 在前端.ss表单中显示recaptcha,将以下代码添加到前端.ss模板的末尾。例如。
$showGoogleRecaptcha.RAW
  • 在控制器PHP文件中验证recaptcha,将以下代码添加到控制器的formAction函数中。例如。
GoogleRecaptcha::verify($GoogleRecaptchaSecretKey, 'Google Recaptcha Validation Failed!!');

Laravel 5.x自定义登录表单的用法示例

  • 首先将其包含在LoginController.php文件中
use GoogleRecaptchaToAnyForm\GoogleRecaptcha;
  • 在LoginController.php中创建一个函数以显示recaptcha。例如。
public function showLoginForm()
{
    $showRecaptcha = GoogleRecaptcha::show('site_key', 'password', 'no_debug', 'mt-4 mb-3 col-md-6 offset-md-4', 'Please tick the reCAPTCHA checkbox first!');
    return view('auth.login', compact('showRecaptcha'));
}
  • 在auth/login.blade.php中显示recaptcha,将以下代码添加到auth/login.blade.php模板的末尾。例如。
{!! $showRecaptcha !!}
  • 在LoginController.php文件中验证recaptcha,为validateLogin添加以下代码。例如。
protected function validateLogin(Request $request)
{

    GoogleRecaptcha::verify('secret_key', 'Google Recaptcha Validation Failed!!');

    $request->validate([
        $this->username() => 'required|string',
        'password' => 'required|string',
    ]);
}

Wordpress自定义表单的用法示例

  • 首先将其包含在自定义表单模板PHP文件中。注意:更改正确的vendor路径以require_once
require_once(__DIR__ . '/../../../../vendor/autoload.php');
use GoogleRecaptchaToAnyForm\GoogleRecaptcha;
  • 在表单模板中显示recaptcha。例如。
echo GoogleRecaptcha::show('site_key', 'input_2_3', 'no_debug', 'mt-4 mb-3 col-md-6 offset-md-4', 'Please tick the reCAPTCHA checkbox first!');
  • 在处理表单提交的方法中验证recaptcha。例如。
require_once(__DIR__ . '/../../vendor/autoload.php');
use GoogleRecaptchaToAnyForm\GoogleRecaptcha;

GoogleRecaptcha::verify('secret_key', 'Google Recaptcha Validation Failed!!');

许可证

  • MIT