fractal512/captcha

Very simple Laravel 5 stateless captcha package.

v1.0.1 2021-02-04 19:08 UTC

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 in fontsDirectory;
  • 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