ami-praha/invisible-recaptcha

Laravel 的隐形 reCAPTCHA。

v2.0.2 2024-06-28 13:00 UTC

README

php-badge packagist-badge Total Downloads travis-badge

invisible_recaptcha_demo

为什么选择隐形 reCAPTCHA?

隐形 reCAPTCHA 是 reCAPTCHA v2(无验证码)的改进版本。在 reCAPTCHA v2 中,用户需要点击按钮:“我不是机器人”来证明他们是人类。在隐形 reCAPTCHA 中,不会嵌入验证码框供用户点击。它完全隐形!仅在页面底部显示徽章,提示用户您的网站正在使用这项技术。(徽章可以隐藏,但建议不要隐藏。)

注意

  • 主分支不支持多验证码功能,如果您需要,请使用 multi-forms 分支。(您尝试在单个页面中放置多个验证码时,大多数情况下是在误用 reCAPTCHA。

安装

composer require albertcht/invisible-recaptcha

Laravel 5

设置

将 ServiceProvider 添加到 app/config/app.php 中的 providers 数组。

AlbertCht\InvisibleReCaptcha\InvisibleReCaptchaServiceProvider::class,

它还支持 Laravel 5.5 的包发现。

配置

在设置您的配置之前,请记住在申请密钥时选择 隐形 reCAPTCHAinvisible_recaptcha_setting

INVISIBLE_RECAPTCHA_SITEKEYINVISIBLE_RECAPTCHA_SECRETKEY 添加到 .env 文件。

// required
INVISIBLE_RECAPTCHA_SITEKEY={siteKey}
INVISIBLE_RECAPTCHA_SECRETKEY={secretKey}

// optional
INVISIBLE_RECAPTCHA_BADGEHIDE=false
INVISIBLE_RECAPTCHA_DATABADGE='bottomright'
INVISIBLE_RECAPTCHA_TIMEOUT=5
INVISIBLE_RECAPTCHA_DEBUG=false

您可以选择三种不同的验证码样式:bottomrightbottomleftinline

如果将 INVISIBLE_RECAPTCHA_BADGEHIDE 设置为 true,您可以隐藏徽章标志。

您可以通过将 INVISIBLE_RECAPTCHA_DEBUG 设置为 true,在浏览器控制台中查看这些验证码元素的绑定状态。

使用方法

在渲染验证码之前,请记住以下注意事项

  • render()renderHTML() 函数必须在表单元素内调用。
  • 您必须确保提交按钮的 type 属性为 submit
  • 您的表单中只能有一个提交按钮。
在您的视图中显示 reCAPTCHA
{!! app('captcha')->render() !!}

// or you can use this in blade
@captcha

支持自定义语言

{!! app('captcha')->render('en') !!}

// or you can use this in blade
@captcha('en')
与 VueJS 等JavaScript 框架一起使用

渲染过程包括三个独立的部分,可以在使用 VueJS 等框架时单独渲染,这些框架在模板中包含 <script> 标签时会抛出控制台错误。

您可以在 HTML 的头部位置渲染 polyfill(这样做)

{!! app('captcha')->renderPolyfill() !!}
// Or with blade directive:
@captchaPolyfill

您可以使用以下方式渲染 HTML,这需要放在您的 <form> 标签内

{!! app('captcha')->renderCaptchaHTML() !!}
// Or with blade directive:
@captchaHTML

您可以使用以下方式渲染必要的 <script> 标签以及可选的语言支持

// The argument is optional.
{!! app('captcha')->renderFooterJS('en') !!}

// Or with blade directive:
@captchaScripts
// blade directive, with language support:
@captchaScripts('en')
验证

'g-recaptcha-response' => 'required|captcha' 添加到规则数组。

$validate = Validator::make(Input::all(), [
    'g-recaptcha-response' => 'required|captcha'
]);

CodeIgniter 3.x

在 application/config/config.php 中设置

$config['composer_autoload'] = TRUE;

在 application/config/config.php 中添加以下行

$config['recaptcha.sitekey'] = 'sitekey'; 
$config['recaptcha.secret'] = 'secretkey';
// optional
$config['recaptcha.options'] = [
    'hideBadge' => false,
    'dataBadge' => 'bottomright',
    'timeout' => 5,
    'debug' => false
];

在控制器中,使用

$data['captcha'] = new \AlbertCht\InvisibleReCaptcha\InvisibleReCaptcha(
    $this->config->item('recaptcha.sitekey'),
    $this->config->item('recaptcha.secret'),
    $this->config->item('recaptcha.options'),
);

在视图中,在您的表单中

<?php echo $captcha->render(); ?>

然后回到您的控制器中,您可以验证它

$captcha->verifyResponse($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);

不使用 Laravel 或 CodeIgniter

请查看下面的示例

<?php

require_once "vendor/autoload.php";

$siteKey = 'sitekey';
$secretKey = 'secretkey';
// optional
$options = [
    'hideBadge' => false,
    'dataBadge' => 'bottomright',
    'timeout' => 5,
    'debug' => false
];
$captcha = new \AlbertCht\InvisibleReCaptcha\InvisibleReCaptcha($siteKey, $secretKey, $options);

// you can override single option config like this
$captcha->setOption('debug', true);

if (!empty($_POST)) {
    var_dump($captcha->verifyResponse($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']));
    exit();
}

?>

<form action="?" method="POST">
    <?php echo $captcha->render(); ?>
    <button type="submit">Submit</button>
</form>

控制提交函数

仅在您需要在点击提交按钮后完全控制时使用此函数。如果您在此函数中返回 false,则不会触发 reCAPTCHA 验证。

_beforeSubmit = function(e) {
    console.log('submit button clicked.');
    // do other things before captcha validation
    // e represents reference to original form submit event
    // return true if you want to continue triggering captcha validation, otherwise return false
    return false;
}

自定义提交函数

如果您想自定义提交函数,例如:在点击提交按钮后执行某些操作或将提交改为 AJAX 调用等。

您需要做的只是实现javascript中的 _submitEvent

_submitEvent = function() {
    console.log('submit button clicked.');
    // write your logic here
    // submit your form
    _submitForm();
}

以下是一个使用 AJAX 提交的示例(使用 jQuery 选择器)

_submitEvent = function() {
    $.ajax({
        type: "POST",
        url: "{{route('message.send')}}",
         data: {
            "name": $("#name").val(),
            "email": $("#email").val(),
            "content": $("#content").val(),
            // important! don't forget to send `g-recaptcha-response`
            "g-recaptcha-response": $("#g-recaptcha-response").val()
        },
        dataType: "json",
        success: function(data) {
            // success logic
        },
        error: function(data) {
            // error logic
        }
    });
};

示例仓库

仓库: https://github.com/albertcht/invisible-recaptcha-example

该仓库演示了如何通过ajax方式使用此包。

展示

致谢

  • anhskohbo(无验证码包的作者)
  • 贡献者

在 Beerpay 上支持

嘿兄弟!帮帮我弄点 🍻!

Beerpay Beerpay