laravel-captcha maintained by haosheng0211
Laravel Captcha
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="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.