nguyendachuy/laravel-recaptcha3
Composer 安装命令:
composer require nguyendachuy/laravel-recaptcha3
包简介
This library provides support for Google reCAPTCHA v3 in Laravel. This library makes it easy to add reCAPTCHA to your Laravel application to protect against spam and bots.
README 文档
README
This library provides support for Google reCAPTCHA v3 in Laravel. This library makes it easy to add reCAPTCHA to your Laravel application to protect against spam and bots.
Table of Contents
Installation
You can install the package via composer:
composer require nguyendachuy/laravel-recaptcha3
You can publish config file with:
php artisan vendor:publish --tag="recaptcha-config"
This is the contents of the published config file:
return [ /* |-------------------------------------------------------------------------- | The reCAPTCHA site key provided by Google |-------------------------------------------------------------------------- | | Here you can set the sitekey */ 'sitekey' => env('GOOGLE_CAPTCHA_SITEKEY', null), /* |-------------------------------------------------------------------------- | The reCAPTCHA secret key provided by Google |-------------------------------------------------------------------------- | | Here you can set the secet */ 'secret' => env('GOOGLE_CAPTCHA_SECRET', null) ];
References
Google reCAPTCHA documentation: https://developers.google.com/recaptcha/docs/v3
Usage
Using the Facade
You can also use the Recaptcha facade directly in your controllers or other classes:
use NguyenHuy\Recaptcha\Facades\Recaptcha; class ContactController extends Controller { public function store(Request $request) { // Verify the token $token = $request->input('g-recaptcha-response'); $ip = $request->ip(); $action = 'contact_form'; $minScore = 0.5; $result = Recaptcha::verify($token, $ip, [$action, $minScore]); if (!$result) { return back()->withErrors(['captcha' => 'reCAPTCHA verification failed']); } // Process form submission // ... } }
Blade directive
This directive imports the recaptcha JavaScript library and configures it with your site key.
<body>
{{-- your app --}}
{{-- Default action is "form" --}}
@recaptchaJs
{{-- or custom action --}}
@recaptchaJs('form')
</body>
Use on the form
<form>
{{-- your input --}}
{{-- Default name is "g-recaptcha-response" --}}
@recaptchaInput
{{-- or custom name --}}
@recaptchaInput('custom-name')
{{-- with custom attributes and action --}}
@php
$attributes = ['data-form' => 'contact'];
$action = 'contact_form';
$name = 'g-recaptcha-response';
@endphp
@recaptchaInput($name)
</form>
Use on the validator
$request->validate([ 'g-recaptcha-response' => 'captcha' ]);
Optimizing Views
php artisan view:clear
Credits
Advanced Features
Auto Token Refresh
reCAPTCHA tokens expire after 2 minutes. This library now automatically refreshes tokens every 90 seconds to ensure your forms always have valid tokens.
Error Handling
Improved error handling with detailed logging when verification fails, making it easier to debug issues.
Custom Attributes
You can now add custom HTML attributes to the reCAPTCHA input field:
$attributes = ['data-form' => 'contact', 'class' => 'recaptcha-input']; $captcha = new \NguyenHuy\Recaptcha\CaptchaV3(); $display = $captcha->display('g-recaptcha-response', $attributes);
Action-specific Validation
You can specify different actions for different forms and validate them accordingly:
// In your form @php $action = 'contact_form'; @endphp @recaptchaInput('g-recaptcha-response') // In your controller $request->validate([ 'g-recaptcha-response' => 'captcha:contact_form,0.7' ]);
Changelog
v1.1.0 (2025-05-26)
- Added auto token refresh to prevent token expiration
- Improved error handling with detailed logging
- Added support for custom HTML attributes
- Fixed Facade binding issue
- Added support for action-specific validation
- Enhanced security with proper input sanitization
v1.0.0 (2023)
- Initial release
Support
Please feel free to contact me if you find any bug or create an issue for that!.
License
The MIT License (MIT). Please see License File for more information.
nguyendachuy/laravel-recaptcha3 适用场景与选型建议
nguyendachuy/laravel-recaptcha3 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 39 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 12 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「recaptcha」 「laravel」 「form validation」 「Google ReCaptcha」 「Laravel reCAPTCHA」 「recaptcha v3」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nguyendachuy/laravel-recaptcha3 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nguyendachuy/laravel-recaptcha3 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nguyendachuy/laravel-recaptcha3 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Client library for reCAPTCHA with proxy support, a free service that protects websites from spam and abuse.
Diese Contao 4 Erweiterung stellt Google reCAPTCHA V2 in Form eines neuen Formularfeldes im Formulargenerator bereit. This extension provides Google reCAPTCHA V2 in the form of a new form field in the form generator of Contao Open Source CMS.
A simple and easy to use Google ReCaptcha v3 package for Laravel
A Laravel Filament Forms slug field.
A jQuery augmented PHP library for creating and validating HTML forms
CakePHP Captcha Plugin, Image, Math and Google Recaptcha Support
统计信息
- 总下载量: 39
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2023-12-07