overlu/mini-captcha

Mini Captcha 包

安装: 28

依赖: 0

建议者: 0

安全性: 0

星标: 1

关注者: 1

分支: 0

开放问题: 0

类型:package

v1.1.3 2022-01-24 11:37 UTC

This package is auto-updated.

Last update: 2024-09-24 17:33:50 UTC


README

预览

Preview

安装

使用 composer 安装此包

composer require overlu/mini-captcha

使用 composer update 更新包或使用 composer install 安装。

在 Windows 上,您需要在 php.ini 中包含 GD2 DLL php_gd2.dll。您还需要包含 php_fileinfo.dllphp_mbstring.dll 以满足 mews/captcha 依赖项的要求。

使用方法

要使用 Captcha 服务提供商,您必须在启动 Laravel 应用程序时注册该提供商。有两种基本方法可以实现。

config/app.php 中找到 providers 键并注册 Captcha 服务提供商。

    'providers' => [
        // ...
        MiniCaptcha\CaptchaServiceProvider::class,
    ]

配置

要使用自己的设置,发布配置。

$ php bin/artisan vendor:publish

config/captcha.php

return [
    'default'   => [
        'length'    => 5,
        'width'     => 120,
        'height'    => 36,
        'quality'   => 90,
        'math'      => true,  //Enable Math Captcha
        'expire'    => 60,    //Stateless/API captcha expiration
    ],
    // ...
];

示例使用

会话模式

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

无状态模式

您可以从此 URL 获取 key 和 img https:///captcha/api/math 并使用此方法验证 captcha

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

返回图像

captcha();

Captcha::create();

返回 URL

captcha_src();

Captcha::src('default');

返回 HTML

captcha_img();

Captcha::img();

要使用不同的配置

captcha_img('flat');

Captcha::img('inverse');

等。

基于 Intervention Image

^_^