recurloop/laravel-recaptcha
Composer 安装命令:
composer require recurloop/laravel-recaptcha
包简介
Laravel package for Recaptcha V3.
关键字:
README 文档
README
Laravel package for Recaptcha V3
The main motivation for this project was the fact that I could not find an existing package for recaptcha v3 that met two conditions:
- possible reading of scores for requests (without rejecting if the score is too low)
- the recaptcha token should be generated just before sending the request (and not when generating the page), which may result in an expired token.
Installation
To get started, use Composer to add the package to your project's dependencies:
composer require recurloop/laravel-recaptcha:dev-main
Add RECAPTCHA_SITE_KEY and RECAPTCHA_SECRET_KEY to your .env file. (You can get them here)
RECAPTCHA_SITE_KEY=sitekey
RECAPTCHA_SECRET_KEY=secret
Optionally, you can publish the config file:
php artisan vendor:publish --provider="RecurLoop\Recaptcha\Providers\RecaptchaServiceProvider"
Usage
Init Recaptcha
recaptcha v3 should be loaded on every page to track user activity:
<head> ... {!! Recaptcha::initJs() !!} </head>
Forms
a recaptcha token should be sent with each form along with the name of the action performed:
<form method="post" action="/contact"> <button onclick="{!! Recaptcha::onClickSubmitJs('contact') !!}">Send message</button> </form>
or
<form method="post" action="/contact" onsubmit="{!! Recaptcha::addTokenJs('contact') !!}"> <button type="submit">Send message</button> </form>
Request validation
Register the middleware in your Kernel.php file.
protected $routeMiddleware = [ ... 'recaptcha' => \App\Http\Middleware\VerifyRecaptchaScore::class, ... ];
You can then reject requests if they do not reach the minimum specified in the config.
Route::post('contact', fn() => null)->middleware('recaptcha:contact');
or with given minimum
Route::post('contact', fn() => null)->middleware('recaptcha:contact,0.7');
Check the score
Alternatively, you can check if the score has been reached directly in the code:
use RecurLoop\Recaptcha\Exceptions\InvalidTokenException; use RecurLoop\Recaptcha\Facades\Recaptcha; try { // Regardless of the action Recaptcha::checkScore(request()); // Including a token (the token will be invalid if the action does not match) Recaptcha::checkScore(request(), 'contact'); // Taking into account the action and the minimum score Recaptcha::checkScore(request(), 'contact', 0.75); } catch (InvalidTokenException $exception) { ... }
Retrieve the score
Alternatively, you can retrieve the score
use RecurLoop\Recaptcha\Exceptions\InvalidTokenException; use RecurLoop\Recaptcha\Facades\Recaptcha; try { // Regardless of the action $score = Recaptcha::retrieveScore(request()); // Including a token (the token will be invalid if the action does not match) $score = Recaptcha::retrieveScore(request(), 'contact'); } catch (InvalidTokenException $exception) { ... }
Hiding the ReCAPTCHA Badge
Add to your CSS file:
.grecaptcha-badge { visibility: hidden !important; }
Localization
By default, the package follows the default application locale, which is defined in config/app.php. If you want to change this behavior, you can specify what locale to use by adding a new environment variable :
RECAPTCHA_LOCALE=en
recurloop/laravel-recaptcha 适用场景与选型建议
recurloop/laravel-recaptcha 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 12 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「recaptcha」 「captcha」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 recurloop/laravel-recaptcha 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 recurloop/laravel-recaptcha 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 recurloop/laravel-recaptcha 相关的其它包
同方向 / 同关键字的高下载量 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
Two Captcha
Laravel 5 Securimage helper
CakePHP Captcha Plugin, Image, Math and Google Recaptcha Support
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-12-11