fractal512 / captcha
Very simple Laravel 5 stateless captcha package.
Installs: 12
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:package
Requires
- php: ^5.6|^7.2|^8.0
- ext-gd: *
- illuminate/config: ~5|^6|^7|^8
- illuminate/support: ~5|^6|^7|^8
Requires (Dev)
- mockery/mockery: ^1.0
- phpunit/phpunit: ^8.5
This package is auto-updated.
Last update: 2025-02-21 02:33:38 UTC
README
A very simple stateless captcha for Laravel 5+. Basically it's a simple Laravel package wrapper for my old captcha script written a long time ago.
Installation
The Captcha can be installed via Composer by requiring the
fractal512/captcha
package and setting the minimum-stability
to dev
(required for Laravel 5) in your
project's composer.json
.
{ "require": { "laravel/framework": "5.0.*", "fractal512/captcha": "^1.0" }, "minimum-stability": "dev" }
Then update your packages with composer update
or install with composer install
.
Or require this package with composer:
composer require fractal512/captcha
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 fractal512/captcha
's dependencies.
Registration in Laravel
No need in versions with auto discovery (Laravel 5.5+).
Register the Captcha Service Provider in the providers
key in config/app.php
.
'providers' => [ // ... 'Fractal512\Captcha\CaptchaServiceProvider', ]
for Laravel 5.1+
'providers' => [ // ... Fractal512\Captcha\CaptchaServiceProvider::class, ]
Register facade for the captcha package in the aliases
key in config/app.php
.
'aliases' => [ // ... 'Captcha' => 'Fractal512\Captcha\Facades\Captcha', ]
for Laravel 5.1+
'aliases' => [ // ... 'Captcha' => Fractal512\Captcha\Facades\Captcha::class, ]
Configuration
Publish the package config.php
file to apply your own settings.
$ php artisan vendor:publish --provider="Fractal512\Captcha\CaptchaServiceProvider" --tag="config"
or run (Laravel 8+)
$ php artisan vendor:publish
then enter the number of the Fractal512\Captcha\CaptchaServiceProvider
service provider.
Contents of the file config/captcha.php
:
return [ 'characters' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', 'expire' => 300, 'attempts' => 10, 'default' => [], 'numbers' => [ 'characters' => '0123456789' ], 'letters' => [ 'characters' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' ], 'uppercase' => [ 'characters' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' ], 'lowercase' => [ 'characters' => 'abcdefghijklmnopqrstuvwxyz' ] ];
Configuration options:
characters
- set of characters used by captcha (available presets:default
,numbers
,letters
,uppercase
,lowercase
);expire
- captcha expiration time in seconds;attempts
- number of attempts per minute to refresh the captcha image.
Additional options may be included:
fontsDirectory
- the fully qualified path without trailing slash to custom fonts directory;fontFile
- custom TTF font file name located infontsDirectory
;captchaDirectory
- the fully qualified path without trailing slash to custom directory storing captcha files.
Usage Example
// base_path() . "/routes/web.php" Route::any('captcha-example', function() { if (request()->getMethod() == 'POST') { $rules = ['captcha' => 'required|captcha']; $validator = validator()->make(request()->all(), $rules); if ($validator->fails()) { echo '<p style="color: #ff0000;">Verification failed!</p>'; } else { echo '<p style="color: #00ff00;">Verification passed!</p>'; } } $form = '<form method="post" action="">'; $form .= '<input type="hidden" name="_token" value="' . csrf_token() . '">'; $form .= '<p>'; $form .= captcha_img('default', ['id' => 'captcha-img']); $form .= '<a href="#" onclick="document.getElementById(\'captcha-img\').src = \'/captcha/default?\' + Date.now()">Refresh</a>'; $form .= '</p>'; $form .= '<p><input type="text" name="captcha"></p>'; $form .= '<p><button type="submit" name="check">Check captcha</button></p>'; $form .= '</form>'; return $form; });
Helpers and Facade
Return Image
captcha();
or using facade
Captcha::create();
Return URL
captcha_src();
or using facade
Captcha::src('default');
Return HTML
captcha_img();
or using facade
Captcha::img();
To use other configurations
captcha_img('numbers'); Captcha::img('uppercase');
etc.
Links
- Wrapper scaffold was taken from Captcha for Laravel 5/6/7