haosheng0211/laravel-captcha

A Laravel package providing unified captcha verification with support for reCAPTCHA, Turnstile, and hCaptcha

Maintainers

Package info

github.com/haosheng0211/laravel-captcha

pkg:composer/haosheng0211/laravel-captcha

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-03-20 16:23 UTC

This package is auto-updated.

Last update: 2026-03-20 16:30:27 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

A Laravel package that provides a unified API for integrating multiple captcha services, including Google reCAPTCHA (v2 & v3), Cloudflare Turnstile, and hCaptcha. Easily switch between providers with minimal code changes.

Installation

You can install the package via composer:

composer require haosheng0211/laravel-captcha

You can publish the config file with:

php artisan vendor:publish --tag="laravel-captcha-config"

This is the contents of the published config file:

return [

    'enabled' => env('CAPTCHA_ENABLED', true),

    'default' => env('CAPTCHA_DRIVER', 'recaptcha'),

    'providers' => [
        'recaptcha' => [
            'version' => env('RECAPTCHA_VERSION', 'v2'),
            'site_key' => env('RECAPTCHA_SITE_KEY'),
            'secret_key' => env('RECAPTCHA_SECRET_KEY'),
            'score' => 0.5,
        ],
        'turnstile' => [
            'site_key' => env('TURNSTILE_SITE_KEY'),
            'secret_key' => env('TURNSTILE_SECRET_KEY'),
        ],
        'hcaptcha' => [
            'site_key' => env('HCAPTCHA_SITE_KEY'),
            'secret_key' => env('HCAPTCHA_SECRET_KEY'),
        ],
    ],

];

Configuration

Add the following environment variables to your .env file based on the captcha provider you want to use:

# Global settings
CAPTCHA_ENABLED=true
CAPTCHA_DRIVER=recaptcha

# Google reCAPTCHA
RECAPTCHA_VERSION=v2
RECAPTCHA_SITE_KEY=your-site-key
RECAPTCHA_SECRET_KEY=your-secret-key

# Cloudflare Turnstile
TURNSTILE_SITE_KEY=your-site-key
TURNSTILE_SECRET_KEY=your-secret-key

# hCaptcha
HCAPTCHA_SITE_KEY=your-site-key
HCAPTCHA_SECRET_KEY=your-secret-key

Usage

Using the Facade

use MrJin\Captcha\Facades\Captcha;

// Verify using the default driver
$result = Captcha::verify($request->input('captcha_token'), $request->ip());

// Verify using a specific driver
$result = Captcha::driver('turnstile')->verify($token, $ip);

// Check if captcha is enabled
if (Captcha::isEnabled()) {
    // ...
}

Using the Validation Rule

use MrJin\Captcha\Rules\CaptchaRule;

$request->validate([
    'captcha_token' => ['required', new CaptchaRule],
]);

// With a specific driver
$request->validate([
    'captcha_token' => ['required', new CaptchaRule('turnstile')],
]);

Supported Drivers

Driver Provider Versions
recaptcha Google reCAPTCHA v2, v3
turnstile Cloudflare Turnstile -
hcaptcha hCaptcha -

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.