Top Related Projects
PHP Captcha library
No CAPTCHA reCAPTCHA For Laravel.
Quick Overview
mewebstudio/captcha is a Laravel package that provides an easy-to-use CAPTCHA solution for web applications. It offers customizable image-based CAPTCHAs to protect forms from spam and automated submissions, enhancing security and user verification.
Pros
- Easy integration with Laravel applications
- Highly customizable CAPTCHA options (length, width, height, colors, etc.)
- Supports multiple CAPTCHA styles (default, math, flat)
- Includes built-in validation rules for Laravel
Cons
- Limited to Laravel framework, not usable in other PHP applications
- Requires GD Library or Imagick for image generation
- May impact user experience for legitimate users
- Not as secure as more advanced CAPTCHA solutions (e.g., reCAPTCHA)
Code Examples
- Basic CAPTCHA generation:
use Mews\Captcha\Facades\Captcha;
$captcha = Captcha::create();
- Customizing CAPTCHA options:
$captcha = Captcha::create('default', [
'length' => 6,
'width' => 200,
'height' => 50,
'quality' => 90,
]);
- Validating CAPTCHA input:
use Illuminate\Support\Facades\Validator;
$validator = Validator::make($request->all(), [
'captcha' => 'required|captcha'
]);
Getting Started
-
Install the package via Composer:
composer require mews/captcha
-
Publish the configuration file:
php artisan vendor:publish --provider="Mews\Captcha\CaptchaServiceProvider" --tag="config"
-
Add CAPTCHA to your form:
<form method="POST" action="/submit"> {!! captcha_img() !!} <input type="text" id="captcha" name="captcha" required> <!-- Other form fields --> </form>
-
Validate the CAPTCHA in your controller:
$request->validate([ 'captcha' => 'required|captcha', // Other validation rules ]);
Competitor Comparisons
PHP Captcha library
Pros of Gregwar/Captcha
- More customizable with a wider range of options for image generation
- Supports multiple output formats (PNG, JPEG, GIF)
- Includes built-in effects like wave distortion and background noise
Cons of Gregwar/Captcha
- Less actively maintained (last update was in 2021)
- Requires GD library, which may not be available on all systems
- Slightly more complex setup process
Code Comparison
Captcha generation in mewebstudio/captcha:
$captcha = Captcha::create('default', true);
Captcha generation in Gregwar/Captcha:
$captcha = new Gregwar\Captcha\CaptchaBuilder;
$captcha->build();
$captcha->output();
Key Differences
- mewebstudio/captcha is more Laravel-focused, while Gregwar/Captcha is framework-agnostic
- Gregwar/Captcha offers more advanced image manipulation features
- mewebstudio/captcha has better integration with Laravel's validation system
Conclusion
Both libraries offer robust CAPTCHA functionality, but they cater to different use cases. mewebstudio/captcha is ideal for Laravel projects seeking seamless integration, while Gregwar/Captcha is better suited for projects requiring more customization and flexibility in CAPTCHA image generation.
No CAPTCHA reCAPTCHA For Laravel.
Pros of no-captcha
- Implements Google's reCAPTCHA v2 and v3, offering more modern and user-friendly CAPTCHA options
- Supports multiple languages out of the box
- Easier integration with Laravel's validation system
Cons of no-captcha
- Limited to Google's reCAPTCHA, while captcha offers more customization options
- Requires an API key from Google, which may not be suitable for all projects
- Less control over the CAPTCHA appearance and behavior
Code Comparison
no-captcha:
{!! NoCaptcha::renderJs() !!}
{!! NoCaptcha::display() !!}
$validator = Validator::make(Input::all(), [
'g-recaptcha-response' => 'required|captcha'
]);
captcha:
{!! captcha_img() !!}
<input type="text" id="captcha" name="captcha">
$validator = Validator::make(Input::all(), [
'captcha' => 'required|captcha'
]);
Both libraries offer easy integration with Laravel, but no-captcha focuses on Google's reCAPTCHA implementation, while captcha provides a more customizable, self-hosted solution. The choice between them depends on project requirements, such as the need for offline functionality, customization, or preference for Google's CAPTCHA system.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Captcha for Laravel 10/11
A simple Laravel 5/6 service provider for including the Captcha for Laravel.
for Laravel 4 Captcha for Laravel Laravel 4
for Laravel 5 to 9 Captcha for Laravel Laravel 4
Preview
Installation
The Captcha Service Provider can be installed via Composer by requiring the
mews/captcha
package and setting the minimum-stability
to dev
(required for Laravel 5) in your
project's composer.json
.
{
"require": {
"laravel/framework": "5.0.*",
"mews/captcha": "~3.0"
},
"minimum-stability": "stable"
}
or
Require this package with composer:
composer require mews/captcha
Update your packages with composer update
or install with composer install
.
In Windows, you'll need to include the GD2 DLL php_gd2.dll
in php.ini. And you also need include php_fileinfo.dll
and php_mbstring.dll
to fit the requirements of mews/captcha
's dependencies.
Usage
To use the Captcha Service Provider, you must register the provider when bootstrapping your Laravel application. There are essentially two ways to do this.
Find the providers
key in config/app.php
and register the Captcha Service Provider.
'providers' => [
// ...
'Mews\Captcha\CaptchaServiceProvider',
]
for Laravel 5.1+
'providers' => [
// ...
Mews\Captcha\CaptchaServiceProvider::class,
]
Find the aliases
key in config/app.php
.
'aliases' => [
// ...
'Captcha' => 'Mews\Captcha\Facades\Captcha',
]
for Laravel 5.1+
'aliases' => [
// ...
'Captcha' => Mews\Captcha\Facades\Captcha::class,
]
For Laravel 11 : you do not need to add the alias, it will be added automatically.
Configuration
Custom settings:
To use your own settings, publish config.
$ php artisan vendor:publish
config/captcha.php
return [
'default' => [
'length' => 5,
'width' => 120,
'height' => 36,
'quality' => 90,
'math' => true, //Enable Math Captcha
'expire' => 60, //Captcha expiration
],
// ...
];
Disable validation:
To disable the captcha validation use CAPTCHA_DISABLE
environment variable. e.g. .env config:
CAPTCHA_DISABLE=true
Example Usage
Session Mode:
// [your site path]/Http/routes.php
Route::any('captcha-test', function() {
if (request()->getMethod() == 'POST') {
$rules = ['captcha' => 'required|captcha'];
$validator = validator()->make(request()->all(), $rules);
if ($validator->fails()) {
echo '<p style="color: #ff0000;">Incorrect!</p>';
} else {
echo '<p style="color: #00ff30;">Matched :)</p>';
}
}
$form = '<form method="post" action="captcha-test">';
$form .= '<input type="hidden" name="_token" value="' . csrf_token() . '">';
$form .= '<p>' . captcha_img() . '</p>';
$form .= '<p><input type="text" name="captcha"></p>';
$form .= '<p><button type="submit" name="check">Check</button></p>';
$form .= '</form>';
return $form;
});
Detailed Example in Laravel way view files
//register.blade.php
<img src="{{ captcha_src() }}" alt="captcha">
<div class="mt-2"></div>
<input
type="text" name="captcha" class="form-control @error('captcha') is-invalid @enderror" placeholder="Please Insert Captch"
>
@error('captcha')
<div class="invalid-feedback">{{ $message }}</div> @enderror
controller files
Validator::make($input, [
'name' => ['required', 'string', 'max:255'],
'email' => [
'required',
'string',
'email',
'max:255',
Rule::unique(User::class),
],
'password' => $this->passwordRules(),
'captcha' => 'required|captcha'
])->validate();
Stateless Mode:
You get key and img from this url
http://localhost/captcha/api/math
and verify the captcha using this method:
//key is the one that you got from json response
// fix validator
// $rules = ['captcha' => 'required|captcha_api:'. request('key')];
$rules = ['captcha' => 'required|captcha_api:'. request('key') . ',math'];
$validator = validator()->make(request()->all(), $rules);
if ($validator->fails()) {
return response()->json([
'message' => 'invalid captcha',
]);
} else {
//do the job
}
Return Image
captcha();
or
Captcha::create();
Return URL
captcha_src();
or
Captcha::src('default');
Return HTML
captcha_img();
or
Captcha::img();
To use different configurations
captcha_img('flat');
Captcha::img('inverse');
etc.
Based on Intervention Image
^_^
Links
Top Related Projects
PHP Captcha library
No CAPTCHA reCAPTCHA For Laravel.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot