giantbits/yii2-crelish-recaptcha
Composer 安装命令:
composer require giantbits/yii2-crelish-recaptcha
包简介
Google reCAPTCHA v3 integration for Yii2 with automatic token refresh. Works seamlessly with AJAX/PJAX forms.
README 文档
README
Google reCAPTCHA v3 integration for Yii2 and Crelish CMS.
Features
- reCAPTCHA v3 support - Invisible verification, no user interaction required
- Automatic token refresh - Solves the common expired token issue with AJAX/PJAX forms
- Score-based validation - Configure minimum score threshold
- PJAX compatible - Works seamlessly with Yii2 PJAX widgets
- Simple integration - Just add widget and validator to your forms
Installation
composer require giantbits/yii2-crelish-recaptcha
Configuration
Add the component to your application config:
'components' => [ 'recaptcha' => [ 'class' => \giantbits\crelish\recaptcha\RecaptchaConfig::class, 'siteKey' => 'YOUR_RECAPTCHA_V3_SITE_KEY', 'secret' => 'YOUR_RECAPTCHA_V3_SECRET_KEY', 'scoreThreshold' => 0.5, // optional, default 0.5 (0.0 = bot, 1.0 = human) ], ]
Or use environment variables:
'recaptcha' => [ 'class' => \giantbits\crelish\recaptcha\RecaptchaConfig::class, 'siteKey' => $_ENV['RECAPTCHA_SITE_KEY'], 'secret' => $_ENV['RECAPTCHA_SECRET_KEY'], ],
Usage
1. Add the attribute to your model
class ContactForm extends Model { public $name; public $email; public $message; public $recaptcha; // Add this attribute public function rules() { return [ [['name', 'email', 'message'], 'required'], ['email', 'email'], // Add the validator ['recaptcha', \giantbits\crelish\recaptcha\RecaptchaValidator::class], ]; } }
2. Add the widget to your form
With ActiveForm (PHP):
<?php $form = ActiveForm::begin(); ?> <?= $form->field($model, 'name') ?> <?= $form->field($model, 'email') ?> <?= $form->field($model, 'message')->textarea() ?> <?= $form->field($model, 'recaptcha')->widget( \giantbits\crelish\recaptcha\RecaptchaWidget::class, ['action' => 'contact_form'] )->label(false) ?> <?= Html::submitButton('Submit') ?> <?php ActiveForm::end(); ?>
With Twig (Crelish CMS):
{{ use('giantbits/crelish/recaptcha/RecaptchaWidget') }}
{% set form = active_form_begin({'id': 'contact-form'}) %}
{{ form.field(model, 'name') | raw }}
{{ form.field(model, 'email') | raw }}
{{ form.field(model, 'message').textarea() | raw }}
{{ form.field(model, 'recaptcha').widget(recaptcha, {'action': 'contact_form'}).label(false) | raw }}
{{ html.submitButton('Submit', {'class': 'btn btn-primary'}) | raw }}
{{ active_form_end() }}
3. Works with PJAX
The widget automatically handles PJAX forms. No additional configuration needed:
<?php Pjax::begin(['id' => 'contact-form-pjax']); ?> <?php $form = ActiveForm::begin(['options' => ['data-pjax' => true]]); ?> // ... form fields ... <?= $form->field($model, 'recaptcha')->widget(RecaptchaWidget::class)->label(false) ?> <?php ActiveForm::end(); ?> <?php Pjax::end(); ?>
Validator Options
['recaptcha', RecaptchaValidator::class, 'scoreThreshold' => 0.7, // Override component threshold 'action' => 'contact_form', // Verify action matches (optional) 'verifyHostname' => true, // Verify hostname (optional) ]
Widget Options
$form->field($model, 'recaptcha')->widget(RecaptchaWidget::class, [ 'action' => 'contact_form', // Action name for Google analytics 'siteKey' => 'override-key', // Override component siteKey (optional) ])
How It Works
Unlike other reCAPTCHA v3 implementations that generate a token on page load (which expires after ~2 minutes), this package:
- Intercepts form submission
- Requests a fresh token from Google
- Injects the token into the form
- Submits the form with the valid token
This solves the common "token expired" issue, especially with AJAX/PJAX forms where users may take longer to fill out the form.
Getting reCAPTCHA v3 Keys
- Go to Google reCAPTCHA Admin
- Register a new site
- Choose reCAPTCHA v3
- Add your domains
- Copy the Site Key and Secret Key
Important: reCAPTCHA v3 keys are different from v2 keys. Make sure you select v3 when creating your keys.
License
MIT License
giantbits/yii2-crelish-recaptcha 适用场景与选型建议
giantbits/yii2-crelish-recaptcha 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 69 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 05 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「recaptcha」 「yii2」 「crelish」 「form-validation」 「spam-protection」 「recaptcha-v3」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 giantbits/yii2-crelish-recaptcha 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 giantbits/yii2-crelish-recaptcha 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 giantbits/yii2-crelish-recaptcha 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A custom URL rule class for Yii 2 which allows to create translated URL rules
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
Content management for the Yii2 framework
CakePHP Captcha Plugin, Image, Math and Google Recaptcha Support
统计信息
- 总下载量: 69
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-05-24