A simple, standalone Laravel CAPTCHA package with multiple styles and difficulty levels. No external dependencies or third-party services required!
- 🎨 Multiple Captcha Types: Image, Math, Text, and Slider captchas
- 🎯 Three Difficulty Levels: Easy, Medium, and Hard
- 🎭 Multiple Visual Styles: Default, Modern, Minimal, and Colorful
- 🔒 Fully Standalone: No Google reCAPTCHA or external services
- 🚀 Easy Integration: Simple Blade components and validation rules
- 📱 Responsive Design: Works perfectly on all devices
- ⚡ Lightweight: Minimal dependencies, maximum performance
- 🎨 Customizable: Extensive configuration options
- 🖼️ SVG Support: No GD Library required when using SVG format
- PHP 8.0 or higher
- Laravel 9.x, 10.x, or 11.x
- GD Library (optional - only required for PNG image captcha)
Note: Starting from version 2.0, you can use SVG captcha which doesn't require GD Library!
Install the package via Composer:
composer require dev-3bdulrahman/laravel-captcha
Publish the configuration file:
php artisan vendor:publish --tag=captcha-config
Publish the assets (CSS, JS):
php artisan vendor:publish --tag=captcha-assets
Optionally, publish the views for customization:
php artisan vendor:publish --tag=captcha-views
<form method="POST" action="/submit">
@csrf
<!-- Your form fields -->
@include('captcha::captcha', ['type' => 'image', 'difficulty' => 'medium'])
<button type="submit">Submit</button>
</form>
use Illuminate\Http\Request;
public function submit(Request $request)
{
$request->validate([
'captcha' => 'required|captcha',
// other validation rules
]);
// Process your form
}
That's it! 🎉
@include('captcha::captcha', [
'type' => 'image',
'difficulty' => 'medium',
'style' => 'modern'
])
@include('captcha::captcha', [
'type' => 'math',
'difficulty' => 'easy'
])
@include('captcha::captcha', [
'type' => 'text',
'difficulty' => 'hard'
])
@include('captcha::captcha', [
'type' => 'slider',
'difficulty' => 'medium'
])
- Easy: Simple challenges, fewer characters/operations
- Medium: Moderate complexity (default)
- Hard: Complex challenges with more noise and difficulty
- default: Classic captcha appearance
- modern: Sleek, contemporary design
- minimal: Clean and simple
- colorful: Vibrant and eye-catching
To use SVG captcha instead of PNG (which requires GD Library), update your configuration:
// config/captcha.php
'image' => [
'use_svg' => true, // Enable SVG format
// ... other settings
],
Or set the environment variable:
CAPTCHA_USE_SVG=true
Then use it normally:
@include('captcha::captcha', [
'type' => 'image',
'difficulty' => 'medium'
])
use Dev3bdulrahman\LaravelCaptcha\Facades\Captcha;
// Generate captcha
$data = Captcha::generate('image', 'medium');
// Verify captcha
$isValid = Captcha::verify($input, 'image');
// Refresh captcha
Captcha::refresh('image');
// Get captcha data
$data = Captcha::getData('image');
use Dev3bdulrahman\LaravelCaptcha\Facades\Captcha;
if (Captcha::verify($request->input('captcha'), 'image')) {
// Captcha is valid
} else {
// Captcha is invalid
}
The package automatically registers these routes:
GET /captcha/generate/{type?}
- Generate captcha dataGET /captcha/image/{type?}
- Get captcha imagePOST /captcha/verify
- Verify captchaGET /captcha/refresh
- Refresh captcha
The configuration file config/captcha.php
allows you to customize:
return [
// Default captcha type
'default' => 'image',
// Default difficulty level
'difficulty' => 'medium',
// Session key for storing captcha
'session_key' => 'laravel_captcha',
// Expiration time in minutes
'expire' => 5,
// Image captcha settings
'image' => [
'width' => 200,
'height' => 60,
'length' => [
'easy' => 4,
'medium' => 5,
'hard' => 6,
],
// ... more settings
],
// Math captcha settings
'math' => [
'operators' => [
'easy' => ['+', '-'],
'medium' => ['+', '-', '*'],
'hard' => ['+', '-', '*', '/'],
],
// ... more settings
],
// ... other settings
];
Edit config/captcha.php
:
'text' => [
'questions' => [
'easy' => [
'Your question?' => 'answer',
// Add more questions
],
],
],
Publish the views and modify the CSS:
php artisan vendor:publish --tag=captcha-views
Then edit the files in resources/views/vendor/captcha/
.
composer test
<form method="POST" action="{{ route('contact.submit') }}">
@csrf
<input type="text" name="name" placeholder="Your Name" required>
<input type="email" name="email" placeholder="Your Email" required>
<textarea name="message" placeholder="Your Message" required></textarea>
@include('captcha::captcha', ['type' => 'math', 'difficulty' => 'easy'])
@error('captcha')
<span class="error">{{ $message }}</span>
@enderror
<button type="submit">Send Message</button>
</form>
<form method="POST" action="{{ route('register') }}">
@csrf
<!-- Registration fields -->
@include('captcha::captcha', [
'type' => 'image',
'difficulty' => 'medium',
'style' => 'modern'
])
<button type="submit">Register</button>
</form>
Contributions are welcome! Please feel free to submit a Pull Request.
This package is open-sourced software licensed under the MIT license.
Abdulrahman Mehesan
- Website: https://3bdulrahman.com/
- GitHub: @Dev-3bdulrahman
If you find this package helpful, please consider giving it a ⭐ on GitHub!
Made with ❤️ by Abdulrahman Mehesan