A modern, highly customizable, and secure captcha package for Laravel.
This package provides an easy way to protect your web forms from bots by generating image-based captchas. It's designed to be both user-friendly for humans and challenging for bots.
text and math captcha generators out of the box.You can install the package via Composer:
composer require mydaniel/laravel-captcha
Publish the configuration and translation files using the following Artisan command:
php artisan vendor:publish --provider="MyDaniel\Captcha\CaptchaServiceProvider"
This will create two new files:
config/captcha.php: Here you can configure all aspects of the captcha, including asset paths, character sets, and image settings for different profiles (default, math, etc.).lang/vendor/captcha/: This directory will contain the translation files for validation messages.Using the captcha involves two steps: displaying it in your form and validating the user's input.
In your Blade view, use the Captcha::create() facade to generate the captcha data.
{{-- Generate the captcha data --}}
@php
$captcha = \MyDaniel\Captcha\Facades\Captcha::create('default'); // Or 'math', 'inverse', etc.
@endphp
<div class="form-group">
{{-- Display the captcha image --}}
<img src="{{ $captcha['image'] }}" alt="Captcha Image">
{{-- Input for the user to type the captcha code --}}
<input type="text" id="captcha_code" name="captcha_code" required>
{{-- Hidden field to store the captcha key --}}
<input type="hidden" name="captcha_key" value="{{ $captcha['key'] }}">
</div>
@error('captcha_code')
<span class="text-danger">{{ $message }}</span>
@enderror
In your controller or form request, use the provided Captcha validation rule.
This method is clean and type-safe.
use Illuminate\Http\Request;
use MyDaniel\Captcha\Rules\Captcha;
public function yourControllerMethod(Request $request)
{
$request->validate([
// ... other fields
'captcha_code' => ['required', new Captcha($request->input('captcha_key'), 'default')],
'captcha_key' => 'required|string',
]);
// Captcha is valid, proceed...
}
You can also use the rule as a string, which is useful in some scenarios.
use Illuminate\Http\Request;
public function yourControllerMethod(Request $request)
{
$request->validate([
// ... other fields
'captcha_code' => 'required|captcha:' . $request->input('captcha_key') . ',default',
'captcha_key' => 'required|string',
]);
// Captcha is valid, proceed...
}
You can easily use your own fonts and background images.
.ttf font files or .png/.jpg background images in any directory within your project (e.g., public/assets/captcha).fonts_path and backgrounds_path in your config/captcha.php file to point to these new directories.The package is built to be extensible. To create a new captcha type (e.g., a question-based captcha):
Create a new class that implements the \MyDaniel\Captcha\Contracts\CaptchaGeneratorContract interface.
Implement the generate(array $config): array method.
Register your new generator in a service provider (e.g., AppServiceProvider):
public function register()
{
$this->app->bind('captcha.generator.question', \App\Captcha\QuestionGenerator::class);
}
You can now use 'driver' => 'question' in a new profile in your config/captcha.php file.
throttle middleware to prevent brute-force attacks.This package is open-sourced software licensed under the MIT license.
How can I help you explore Laravel packages today?