定制 bkstar123/laravel-recaptcha 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bkstar123/laravel-recaptcha

Composer 安装命令:

composer require bkstar123/laravel-recaptcha

包简介

Google reCaptcha v2 checkbox & v3 validation

README 文档

README

This package adds reCaptcha_v2 Checkbox and reCaptcha_v3 validation. Both versions of reCaptcha can be used together

1.Requirements

It is recommended to install this package with PHP version 7.1.3+ and Laravel Framework version 5.5+

2.Installation

composer require bkstar123/laravel-recaptcha 

For reCaptcha_v2 Checkbox, add the following settings in the .env file:
GOOGLE_RECAPTCHA_KEY=<your-google-recaptcha-v2-key>
GOOGLE_RECAPTCHA_SECRET=<your-google-recpatcha-v2-secret>

For reCaptcha_v3, add the following settings in the .env file:
GOOGLE_RECAPTCHA3_KEY=<your-google-recaptcha-v3-key>
GOOGLE_RECAPTCHA3_SECRET=<your-google-recpatcha-v3-secret>
GOOGLE_RECAPTCHA3_THRESHOLD=<your-desired-score-to-decide-this-is-a-human> (optional, by default set to 0.5)

3. Usage for reCaptcha v2 checkbox

3.1 In Blade view

You can add Google reCaptcha v2 validation to your site by simply calling {{ Recaptcha::addClient() }}

Note: Recaptcha alias is automatically registered, so you do not need to add it in the config/app.php

The following input will be automatically added to your view and being validated in the validation logic

<textarea id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response"></textarea>

You can also display the validation result to the view as follows (use CSS styling to highlight the validation error):

<div class="form-group{{ $errors->has('g-recaptcha-response') ? ' has-error' : '' }}">
    <div class="col-md-12">
        {{ Recaptcha::addClient() }}
        @if ($errors->has('g-recaptcha-response'))
            <span class="help-block">
                <strong>{{ $errors->first('g-recaptcha-response') }}</strong>
            </span>
        @endif
    </div>
</div>

3.2 In validation logic

You can use a new custom validation rule recaptcha against the attribute g-recaptcha-response, for example:

$request->validate([
    'g-recaptcha-response' => 'required|recaptcha'
], [
    'g-recaptcha-response.recaptcha' => 'Please make sure you are not a robot',
    'g-recaptcha-response.required' => 'Re-captcha field is required'
]);

4. Usage for reCaptcha v3

4.1 In Blade view

Firstly, you need to call addClient3Html() method on Recaptcha facade, for example:

{{ Recaptcha::addClient3Html('recaptcha3') }}

This call will create a hidden input as follows:

<input type="hidden" name="recaptcha3" id="recaptcha3">

You can display the validation error by adding the following snippet:

@if ($errors->has('recaptcha3'))
    <span class="help-block">
        <strong>{{ $errors->first('recaptcha3') }}</strong>
    </span>
@endif

Secondly, you need to call addClient3Js() method on Recaptcha facade, for example:

{{ Recaptcha::addClient3Js('<action_name>', 'recaptcha3') }} 

This call will create a Javascript block as follows:

<script>
    grecaptcha.ready(function() {
        grecaptcha.execute('<SITE-KEY>', {action: '<action_name>'})
                  .then(function (token) {
                    if (token) {
                        document.getElementById('recaptcha3').value = token;
                    }
                  });
    });
</script>

4.2 In Validation logic

You can use a new custom validation rule recaptcha_v3 against the attribute recaptcha3, for example:

$request->validate([
    'recaptcha3' => 'recaptcha_v3'
], ['recaptcha_v3' => 'You are not a human']);

Note:

  • You can give an arbitrary name for action_name as long as it contains only a-z, A-Z and _
  • Also for attribute name, you can use an arbitrary string other than recaptcha3

bkstar123/laravel-recaptcha 适用场景与选型建议

bkstar123/laravel-recaptcha 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 70 次下载、GitHub Stars 达 2, 最近一次更新时间为 2019 年 06 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「validator」 「validation」 「laravel」 「laravel-recaptcha」 「reCaptcha_V2」 「reCaptcha_V3」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 bkstar123/laravel-recaptcha 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 bkstar123/laravel-recaptcha 我们能提供哪些服务?
定制开发 / 二次开发

基于 bkstar123/laravel-recaptcha 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 70
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 10
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-06-09