定制 nguyendachuy/laravel-recaptcha3 二次开发

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

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

nguyendachuy/laravel-recaptcha3

最新稳定版本:v1.0.1

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.

Latest Stable Version Total Downloads Latest Unstable Version License

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.

统计信息

  • 总下载量: 39
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2023-12-07