dev-3bdulrahman/laravel-captcha
Composer 安装命令:
composer require dev-3bdulrahman/laravel-captcha
包简介
A simple, standalone Laravel CAPTCHA package with multiple styles and difficulty levels
README 文档
README
A simple, standalone Laravel CAPTCHA package with multiple styles and difficulty levels. No external dependencies or third-party services required!
✨ Features
- 🎨 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
📋 Requirements
- 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!
📦 Installation
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
🚀 Quick Start
1. Add Captcha to Your Form
<form method="POST" action="/submit"> @csrf <!-- Your form fields --> @include('captcha::captcha', ['type' => 'image', 'difficulty' => 'medium']) <button type="submit">Submit</button> </form>
2. Validate the Captcha
use Illuminate\Http\Request; public function submit(Request $request) { $request->validate([ 'captcha' => 'required|captcha', // other validation rules ]); // Process your form }
That's it! 🎉
📚 Usage
Captcha Types
Image Captcha (Default)
@include('captcha::captcha', [ 'type' => 'image', 'difficulty' => 'medium', 'style' => 'modern' ])
Math Captcha
@include('captcha::captcha', [ 'type' => 'math', 'difficulty' => 'easy' ])
Text Captcha
@include('captcha::captcha', [ 'type' => 'text', 'difficulty' => 'hard' ])
Slider Captcha
@include('captcha::captcha', [ 'type' => 'slider', 'difficulty' => 'medium' ])
Difficulty Levels
- Easy: Simple challenges, fewer characters/operations
- Medium: Moderate complexity (default)
- Hard: Complex challenges with more noise and difficulty
Visual Styles
- default: Classic captcha appearance
- modern: Sleek, contemporary design
- minimal: Clean and simple
- colorful: Vibrant and eye-catching
Using SVG Captcha (No GD Library Required)
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' ])
Using the Facade
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');
Manual Validation
use Dev3bdulrahman\LaravelCaptcha\Facades\Captcha; if (Captcha::verify($request->input('captcha'), 'image')) { // Captcha is valid } else { // Captcha is invalid }
API Routes
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
⚙️ Configuration
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 ];
🎨 Customization
Custom Questions for Text Captcha
Edit config/captcha.php:
'text' => [ 'questions' => [ 'easy' => [ 'Your question?' => 'answer', // Add more questions ], ], ],
Custom Styling
Publish the views and modify the CSS:
php artisan vendor:publish --tag=captcha-views
Then edit the files in resources/views/vendor/captcha/.
🧪 Testing
composer test
📖 Examples
Example 1: Contact Form
<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>
Example 2: Registration 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>
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
📝 License
This package is open-sourced software licensed under the MIT license.
👨💻 Author
Abdulrahman Mehesan
- Website: https://3bdulrahman.com/
- GitHub: @Dev-3bdulrahman
🙏 Support
If you find this package helpful, please consider giving it a ⭐ on GitHub!
📸 Screenshots
Image Captcha
Math Captcha
Text Captcha
Slider Captcha
Made with ❤️ by Abdulrahman Mehesan
dev-3bdulrahman/laravel-captcha 适用场景与选型建议
dev-3bdulrahman/laravel-captcha 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 509 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 10 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「security」 「validation」 「captcha」 「laravel」 「spam-protection」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dev-3bdulrahman/laravel-captcha 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dev-3bdulrahman/laravel-captcha 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dev-3bdulrahman/laravel-captcha 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Diese Contao 4 Erweiterung stellt Google reCAPTCHA V2 in Form eines neuen Formularfeldes im Formulargenerator bereit. This extension provides Google reCAPTCHA V2 in the form of a new form field in the form generator of Contao Open Source CMS.
Provide a way to secure accesses to all routes of an symfony application.
Two Captcha
It's a barebone security class written on PHP
Adds request-parameter validation to the SLIM 3.x PHP framework
A jQuery augmented PHP library for creating and validating HTML forms
统计信息
- 总下载量: 509
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-01



