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_nameas 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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bkstar123/laravel-recaptcha 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Runn Me! Validation and Sanitization Library
Extension for Opis JSON Schema
Adds request-parameter validation to the SLIM 3.x PHP framework
A jQuery augmented PHP library for creating and validating HTML forms
A Laravel validator for delimiter-separated list of emails.
A CakePHP behavior to validate foreign keys
统计信息
- 总下载量: 70
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 10
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-06-09