aronlabs/captcha
Lightweight, secure CAPTCHA for Laravel 10+ (PHP 8.1+) with text and math challenges, Ajax refresh, and Blade includes. Easy validation via CaptchaRule, plus publishable config, views, and fonts for full customization.
Installation
composer require pakdel8463/aron-captcha
Publish the config file:
php artisan vendor:publish --provider="Pakdel8463\AronCaptcha\AronCaptchaServiceProvider" --tag="config"
Basic Usage Add the Captcha field to a form:
<form method="POST" action="/submit">
@csrf
{!! captcha() !!}
<!-- Your form fields -->
</form>
Validation Validate the Captcha in your controller:
use Pakdel8463\AronCaptcha\Facades\AronCaptcha;
public function store(Request $request) {
$request->validate([
'captcha' => 'required|captcha',
]);
// Proceed with form submission
}
First Use Case Secure a contact form or registration page by adding Captcha to prevent spam.
Dynamic Captcha Generation Generate a new Captcha on demand (e.g., after a failed attempt):
$captcha = AronCaptcha::generate();
return view('form', ['captcha' => $captcha]);
Customizing Captcha Appearance
Override default settings in config/aron-captcha.php:
'width' => 200,
'height' => 80,
'length' => 6,
'font_size' => 20,
'colors' => ['background' => '#f5f5f5', 'text' => ['#000000', '#333333']],
Integration with Forms Use the Blade directive for simplicity:
@captcha(['width' => 150, 'height' => 60])
Or manually in JavaScript:
fetch('/captcha/generate')
.then(response => response.text())
.then(data => {
document.getElementById('captcha-container').innerHTML = data;
});
API-Based Captcha For SPAs or APIs, use the endpoint:
Route::get('/captcha/generate', [AronCaptchaController::class, 'generate']);
Validate via:
$request->validate(['captcha' => 'required|captcha:api']);
Rate Limiting Combine with Laravel’s rate limiting middleware:
Route::middleware(['throttle:5,1'])->group(function () {
Route::post('/submit', [YourController::class, 'store']);
});
Captcha Not Validating
captcha rule is correctly imported (use Pakdel8463\AronCaptcha\Rules\Captcha;).captcha.storage) matches the validation rule.Session Storage Issues
AronCaptcha::clear();
Case Sensitivity
'case_sensitive' => false,
Performance with High Traffic
Custom Storage Backends Extend the package by binding a custom storage handler:
AronCaptcha::extend('redis', function ($app) {
return new \Pakdel8463\AronCaptcha\Storage\RedisStorage(
$app->make('redis')
);
});
Then use it in config:
'driver' => 'redis',
Testing Mock the Captcha in tests:
$this->partialMock(AronCaptcha::class, ['generate'])
->shouldReceive('generate')
->andReturn(['code' => 'TEST123']);
Accessibility Add a "refresh Captcha" button with ARIA labels:
<button onclick="refreshCaptcha()" aria-label="Refresh CAPTCHA">
<span>↻</span>
</button>
Logging Failed Attempts Log invalid attempts to monitor abuse:
try {
$request->validate(['captcha' => 'required|captcha']);
} catch (\Illuminate\Validation\ValidationException $e) {
\Log::warning('Failed CAPTCHA attempt', ['ip' => $request->ip()]);
throw $e;
}
Theming
Override the Blade view (resources/views/vendor/aron-captcha.blade.php) to match your app’s design.
Multi-Language Support
Localize error messages in resources/lang/{locale}/validation.php:
'captcha' => 'The CAPTCHA code is incorrect.',
How can I help you explore Laravel packages today?