superpx-cn/laravel-captcha

Laravel Captcha Package

1.0.2 2023-02-23 07:54 UTC

This package is auto-updated.

Last update: 2024-04-23 10:38:30 UTC


README

支持 Laravel 5/6/7/8/9

一个简单的Laravel 图形验证码服务提供商。

预览

Preview

安装

使用 composer 安装

composer require superpx-cn/laravel-captcha

使用

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,
    ]

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;
    });

无状态模式:

你可以使用 助手函数 captcha() 来生成一个验证码 captcha() 方法将会返回一个数组,其中包含了:

  • sensitive
  • key: 验证码的标识
  • img: 验证码的 base64编码

并且使用次方法检查验证码

    // 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