Jerry's WIKIJerry's WIKI
Overview
  • ๐Ÿž Web
  • ๐Ÿ“ Components
  • ๐Ÿ’ก Skills
  • ๐ŸŽฑ Specification
  • ๐Ÿ–ฅ Workflows
  • ๐Ÿ›  Tools
  • ๐ŸŒ๏ธ Linux
  • ๐Ÿ“ฆ Container
  • โ™จ๏ธ Language
Coffee
  • ็ฎ€ไฝ“ไธญๆ–‡
  • English
GitHub
Overview
  • ๐Ÿž Web
  • ๐Ÿ“ Components
  • ๐Ÿ’ก Skills
  • ๐ŸŽฑ Specification
  • ๐Ÿ–ฅ Workflows
  • ๐Ÿ›  Tools
  • ๐ŸŒ๏ธ Linux
  • ๐Ÿ“ฆ Container
  • โ™จ๏ธ Language
Coffee
  • ็ฎ€ไฝ“ไธญๆ–‡
  • English
GitHub
  • ๐Ÿ–ผ Images

    • Qrcode
    • Barcode
    • Captcha
  • ๐Ÿ” Locks

    • Redis Lock
    • Database Pessimistic Locking
    • Database Optimistic Locking
    • Queue(One Customer)
  • ๐Ÿข Offices

    • Export Excel
    • Export Csv
  • โ†”๏ธ Encrypt

    • AES
    • RSA
    • AWS4
    • RC4
  • ๐Ÿช Login

    • JWT
    • Cookie
    • Session
    • Q&A
  • ๐Ÿ“€ Servers

    • Server Notice
    • Deployment Process

Captcha

Index

  • Install Dependencies
  • Encapsulate Utility Class
  • Usage

Install Dependencies

Standard Library Address

ใ€Warningใ€‘

If PHP8.0+, the latest version of the dependency package needs to be loaded. For more details, refer to: issues#110.

composer require gregwar/captcha

Encapsulate Utility Class

ๆŸฅ็œ‹ไปฃ็ 
<?php

declare(strict_types=1);
namespace App\Lib\Image;

use Gregwar\Captcha\CaptchaBuilder;
use Gregwar\Captcha\PhraseBuilder;
use Hyperf\Context\ApplicationContext;
use Hyperf\Redis\Redis;

class Captcha
{
    /**
     * redis ๅฎžไพ‹.
     * @var mixed|Redis Redisๅฎžไพ‹
     */
    private Redis $redis;

    public function __construct()
    {
        $this->redis = ApplicationContext::getContainer()->get(Redis::class);
    }

    /**
     * ่Žทๅ–้ชŒ่ฏ
     * @param string $clientUniqueCode ไธๅŒ็š„ๅฎขๆˆท็ซฏไฝฟ็”จไธๅŒ็š„ๅ”ฏไธ€ๆ ‡่ฏ†
     * @return string ๆ–‡ไปถๆตๅญ—็ฌฆไธฒ
     */
    public function getStream(string $clientUniqueCode): string
    {
        // ้ชŒ่ฏ็ ้•ฟๅบฆๅ’Œ่Œƒๅ›ด
        $phrase = new PhraseBuilder(4, 'abcdefghijklmnpqrstuvwxyz123456789');
        // ๅˆๅง‹ๅŒ–้ชŒ่ฏ็ 
        $builder = new CaptchaBuilder(null, $phrase);
        // ๅˆ›ๅปบ้ชŒ่ฏ็ 
        $builder->build();
        // ่Žทๅ–้ชŒ่ฏ็ ๅ†…ๅฎน
        $phrase = $builder->getPhrase();
        $this->redis->del($clientUniqueCode);
        $this->redis->set($clientUniqueCode, $phrase, ['NX', 'EX' => 300]);

        return $builder->get();
    }

    /**
     * ้ชŒ่ฏ้ชŒ่ฏ็ 
     * @param string $captcha ้ชŒ่ฏ็ 
     * @param string $clientUniqueCode ๅ”ฏไธ€็ 
     * @return bool ๆ˜ฏๅฆ้ชŒ่ฏ้€š่ฟ‡
     */
    public function verify(string $captcha, string $clientUniqueCode): bool
    {
        $cachedCaptcha = $this->redis->get($clientUniqueCode);
        if ($cachedCaptcha === $captcha) {
            $this->redis->del($clientUniqueCode);
            return true;
        }
        return false;
    }
}

Usage

/**
 * ่Žทๅ–้ชŒ่ฏ็ .
 * @param ImageRequest $request ้ชŒ่ฏ่ฏทๆฑ‚็ฑป
 * @return MessageInterface|ResponseInterface ๆตๅผๅ“ๅบ”
 */
#[Scene(scene: 'captcha')]
#[GetMapping(path: 'captcha/show')]
public function getCaptcha(ImageRequest $request): MessageInterface|ResponseInterface
{
    $ip = $this->getRequestIp();
    $uniqueCode = $request->input('captcha_unique_code');
    $unique = $ip . '_' . $uniqueCode;
    $captchaString = (new Captcha())->getStream($unique);

    return $this->response->withHeader('Content-Type', 'image/png')
        ->withBody(new SwooleStream($captchaString));
}

/**
 * ้ชŒ่ฏ้ชŒ่ฏ็ .
 * @param ImageRequest $request ้ชŒ่ฏ่ฏทๆฑ‚็ฑป
 * @return array ['code' => '200', 'msg' => 'ok', 'status' => true, 'data' => []]
 */
#[Scene(scene: 'verify')]
#[GetMapping(path: 'captcha/verify')]
public function verifyCaptcha(ImageRequest $request): array
{
    $ip = $this->getRequestIp();
    $uniqueCode = $request->input('captcha_unique_code');
    $unique = $ip . '_' . $uniqueCode;
    $captchaCode = $request->input('captcha');

    $isSuccess = (new Captcha())->verify($captchaCode, $unique);
    return $this->result->setData(['is_success' => $isSuccess])->getResult();
}
Edit this page
Update At:
Contributor: ็”ฐๆœๅธ†, JerryTZF
Prev
Barcode
Next
Redis Lock