Webman quickly creates a verification code tool similar to Google verification code

Overview

webman-captcha-grid

webman quickly creates a verification code tool similar to Google verification code

webman 快速创建一个类似于 Google 点图验证码的本地验证码扩展

介绍

webman-gridCaptcha 生成类似于谷歌点图验证码的小扩展,因为现在PHP大部分生成的验证码,对于恶意者来说很容易识别,而这套小扩展很简单但是对于机器人来说需要进行深度的机器学习,恶意者攻击的成本也就增加了,但是这套小扩展不同于谷歌验证码需要机器学习,只需要在本地配置好相应的文件即可。因为生成的验证码图片都是读取文件进行生成,所以建议使用Redis进行缓存,代码默认有使用缓存。

ps: 如有不足之处,欢迎大佬提出修改意见。

预览

Preview

安装

 composer require yzh52521/webman-captcha-grid

配置项说明

return [
    //生成验证码图片配置
    'image' => [
        //验证码图片路径
        'path' => env('GRID_CAPTCHA_IMAGE_PATH', public_path().'/storage/gridcaptcha/image'),
        //从验证码图片路径中获取的文件后缀名
        'suffix' => env('GRID_CAPTCHA_IMAGE_SUFFIX', 'jpg'),
        //生成验证码质量
        'quality' => env('GRID_CAPTCHA_IMAGE_QUALITY', 70),
        //生产验证码宽
        'wide' => env('GRID_CAPTCHA_IMAGE_WIDE', 300),
        //生产验证码高
        'high' => env('GRID_CAPTCHA_IMAGE_HIGH', 300),
    ],
    //验证码配置
    'captcha' => [
        //生成的验证码过期时间 单位秒
        'validity' => env('GRID_CAPTCHA_IMAGE_VALIDITY', 180),
        //验证码缓存的key
        'cache_key' => env('GRID_CAPTCHA_IMAGE_CACHE_KEY', 'grid_captcha'),
        //验证码生成的key长度
        'key_length' => env('GRID_CAPTCHA_IMAGE_KEY_LENGTH', 64),
        //自定义效验验证码key字段
        'key_string' => env('GRID_CAPTCHA_IMAGE_KEY_STRING', 'captcha_key'),
        //自定义效验验证码code字段
        'code_string' => env('GRID_CAPTCHA_IMAGE_CODE_STRING', 'captcha_code'),
    ],
];

使用

  • 生成验证码



namespace app\controller;


class Test
{
   
    /**
     * 生成验证码
     * @return array
     */
    public function captcha()
    {
     
        return json(\yzh52521\GridCaptcha\facade\GridCaptcha::get([
            'mobile' => '100xxxxx121'
        ]));
    }
}
  • 生成结果
{
  "hint": "猴子",//提示文本
  "captcha_key": "Qh8kHYF4C....",//验证码key
  "image": "data:image/jpeg;base64,/9j/...."//base64验证码图片 -- 前端渲染显示
}
  • 效验验证码
">
 
<div>
    
    <img src="data:image/jpeg;base64...." width="300" height="300" alt="" style="display: block;">
    <div id="0">div>
    <div id="1">div>
    <div id="2">div>
    <div id="3">div>
    <div id="4">div>
    <div id="5">div>
    <div id="6">div>
    <div id="7">div>
    <div id="8">div>
div>
  • 效果: Preview
namespace app\controller;


use support\Request;

class Test 
{

    
    /**
     * 效验
     * @param Request $request
     * @return array|false
     */
    public function check(Request $request)
    {
      
        /**
         * 传参效验
         */
        if ($captcha_data = \yzh52521\GridCaptcha\facade\GridCaptcha::check('Qh8kHYF4C....', '1540') === false) {
            return json(['message' => '验证码错误', 'code' => 401]);
        }
        /**
         * 传递 Request 对象效验
         */
        if ($captcha_data = \yzh52521\GridCaptcha\facade\GridCaptcha::checkRequest($request)) {
            return json(['message' => '验证码错误', 'code' => 401]);
        }

        return $captcha_data;
    }
}

    //效验完成正确后 您可以进行业务逻辑处理,比如可以获取到上方设置在验证码中的数据 如:上方设置的是手机号,您这里可以获取验证码中的手机号,当效验成功发送短信验证码等...
  • 效验成功返回: 返回的是您在生成验证时传递的数据,默认返回空数组
  • 效验失败返回: false
{
  "mobile" : "100xxxxx121"
}
  • 本地化提示

具体使用 https://www.workerman.net/doc/webman/components/translation.html

resource/translations/zh_CN/messages.php

//一个图片目录对应一个提示
return [
 'grid-captcha' => [
    'banmaxian' => '斑马线',
    'gongjiaoche' => '公交车',
    'heiban' => '黑板',
    'honglvdeng' => '红绿灯',
    'hongzao' => '红枣',
    'houzi' => '猴子',
    'qianbi' => '铅笔',
    'shutiao' => '薯条',
    'xiaofangshuan' => '消防栓',
    'zhenglong' => '蒸笼',
    ]
];
  • 新增验证码图片

    例:新增一个类型为 pingguo 验证码类型的图片,需要在配置文件中的 image.path 目录下创建名为 pingguo 的目录并且把相关类型的图片文件存放在 pingguo 目录,新增一个类型至少要有四张相关类型的图片,不限制文件名,只要文件后缀名是配置文件中指定的即可如下:

─storage
    └─grid-captcha
        └─image
            ├─pingguo
            │       1.jpg
            │       10.jpg
            │       11.jpg
            │       12.jpg
            │       13.jpg
            

License

MIT

You might also like...
Laravel-hours-helper - Creates a Collection of times with a given interval.

Laravel Hours Helper With laravel-hours-helper you can create a collection of dates and/of times with a specific interval (in minutes) for a specific

PHPench creates a graphical output for a PHP benchmark
PHPench creates a graphical output for a PHP benchmark

PHPench creates a graphical output for a PHP benchmark. Plot the runtime of any function in realtime with GnuPlot and create an image out of the result.

An Alfred 4 workflow for quickly opening directories in Visual Studio Code
An Alfred 4 workflow for quickly opening directories in Visual Studio Code

Alfred Workflow - Open in Visual Studio Code An Alfred 4 workflow opening files or folders with Visual Studio Code. Note: Alfred Powerpack is required

A few Fat-Free specific extensions for Tracy Debugger to help debug your code quickly.

Fat-Free Tracy Extensions This is a set of extensions to make working with Fat-Free a little richer. F3 - Analyze all hive variables. Database - Analy

Fact Extraction and VERification Over Unstructured and Structured information

Repository for Fact Extraction and VERification Over Unstructured and Structured information (FEVEROUS), used for the FEVER Workshop Shared Task at EMNLP2021.

The Phar Installation and Verification Environment (PHIVE)

The Phar Installation and Verification Environment (PHIVE) Installation and verification of phar distributed PHP applications has never been this easy

AppGallery IAP is a PHP library to handle AppGallery purchase verification and Server Notifications

AppGallery IAP About AppGallery IAP is a PHP library to handle AppGallery purchase verification and Server Notifications. This package simplifies deve

PHP_Depend is an adaptation of the established Java development tool JDepend. This tool shows you the quality of your design in terms of extensibility, reusability and maintainability.

PHP Depend Documentation PHP Depend for enterprise Available as part of the Tidelift Subscription. The maintainers of PHP Depend and thousands of othe

⚡️ A WordPress plugin to quickly send a 404 for missing static files

Static 404 A WordPress plugin to quickly send a 404 for missing static files. Quickly output a 404 for static files that aren't found, rather than loa

Releases(v1.0.6)
Owner
听风吹雨
听风吹雨
Very easy to use a current limiting component, the code is very simple, based on the webman framework.

Very easy to use a current limiting component, the code is very simple, based on the webman framework.

nsp-team 13 Dec 29, 2022
A lightweight queue based on Redis Stream for webman plugin.

workbunny/webman-rqueue ?? A lightweight queue based on Redis Stream for webman plugin. ?? A lightweight queue based on Redis Stream for webman plugin

workbunny 10 Dec 12, 2022
A PHP implementation of RabbitMQ Client for webman plugin.

workbunny/webman-rabbitmq ?? A PHP implementation of RabbitMQ Client for webman plugin. ?? A PHP implementation of RabbitMQ Client for webman plugin 常

workbunny 15 Dec 15, 2022
Laravel illuminate/filesystem for webman

webman-tech/laravel-filesystem Laravel illuminate/filesystem for webman 介绍 站在巨人(laravel)的肩膀上使文件存储使用更加可靠和便捷 所有方法和配置与 laravel 几乎一模一样,因此使用方式完全参考 Laravel文

null 5 Dec 15, 2022
This package is aimed to be a simplistic PHP workflow package that works in a similar fashion to GitHub Actions.

Workflow This package is aimed to be a simplistic PHP workflow package that works in a similar fashion to GitHub Actions. Installation To install this

Steve McDougall 4 Sep 26, 2022
A tool that allows to quickly export data from Magento 1 and Magento 2 store and import it back into Magento 2

Simple Import / Export tool A tool that allows to quickly export data from Magento 1 and Magento 2 store and import it back into Magento 2. Table data

EcomDev B.V. 51 Dec 5, 2022
Creates a WorldBorder for PocketMine-MP servers!

This plugin is not yet ready for Poggit or download It is going through mass changes over the next few days Message my Discord to recieve an older eli

Soulz 9 Sep 5, 2021
Analyzes PHPStan baseline files and creates aggregated error trend-reports

Analyzes phpstan baseline files Analyzes PHPStan baseline files and creates aggregated error trend-reports.

Markus Staab 22 Dec 23, 2022
A plugin that creates a level system linked to chatting for PocketMine-MP!

ChatLevel A plugin that creates a level system linked to chatting for PocketMine-MP! Issues You can report bugs by simply clicking me! Support You can

Oğuzhan 2 Oct 28, 2021
Creates Packagist.org mirror site.

Packagist Mirror Creates your own packagist.org mirror site. Requirements PHP ^7.1.3 Installation Clone the repository Install dependencies: php compo

Indra Gunawan 32 Mar 30, 2020