easyswoole 验证码使用

简介: easyswoole 验证码使用

easyswoole验证码组件,安装:

easyswoole/verifycode

调用方法:

$config = new Conf();
$code = new \\EasySwoole\\VerifyCode\\VerifyCode($config);
$code->DrawCode();//生成验证码,返回一个Result对象

在easyswoole控制器中使用:

use EasySwoole\\Utility\\Random;
use EasySwoole\\VerifyCode\\Conf;
use EasySwoole\\VerifyCode\\VerifyCode as VerifyCodeModel;
function verifyCode()
{
    $ttl = 120;
    $conf = new Conf();
    $VCode = new VerifyCodeModel($conf);
    // 随机生成验证码
    $random = Random::character(4, '1234567890abcdefghijklmnopqrstuvwxyz');
    $code = $VCode->DrawCode($random);
    $result = \[
        'verifyCode'     => $code->getImageBase64(),
        'verifyCodeTime' => time(),
    \];
    $this->writeJson(200, $result, 'success');
}

注意,需要自己保存验证码,这样用户才能验证成功

可通过session组件,或者使用另一种验证方式进行验证:关于验证码存储机制的转变

关于详细的配置:


<?php// +----------------------------------------------------------------------// | easySwoole \[ use swoole easily just like echo "hello world" \]// +----------------------------------------------------------------------// | WebSite: https://www.easyswoole.com// +----------------------------------------------------------------------// | Welcome Join QQGroup 633921431// +----------------------------------------------------------------------namespace EasySwoole\\VerifyCode;use EasySwoole\\Spl\\SplBean;/**
 * 验证码配置文件
 * Class VerifyCodeConf
 * @author  : evalor <master@evalor.cn>
 * @package Vendor\\VerifyCode
 */class Conf extends SplBean{
    public $charset   = '1234567890AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'; // 字母表
    public $useCurve  = false;         // 混淆曲线
    public $useNoise  = false;         // 随机噪点
    public $useFont   = null;          // 指定字体
    public $fontColor = null;          // 字体颜色
    public $backColor = null;          // 背景颜色
    public $imageL    = null;          // 图片宽度
    public $imageH    = null;          // 图片高度
    public $fonts     = \[\];            // 额外字体
    public $fontSize  = 25;            // 字体大小
    public $length    = 4;             // 生成位数
    public $mime      = MIME::PNG;     // 设置类型
    public $temp      = '/tmp';  // 设置缓存目录
    public function setTemp($temp){
        if (!is_dir($temp)) mkdir($temp,0755) && chmod($temp,0755);
        $this->temp = $temp;
    }
    /**
     * 设置图片格式
     * @param $MimeType
     * @author : evalor <master@evalor.cn>
     * @return Conf
     */
    public function setMimeType($MimeType)
    {
        $allowMime = \[ MIME::PNG, MIME::GIF, MIME::JPG \];
        if (in_array($MimeType, $allowMime)) $this->mime = $MimeType;
        return $this;
    }
    /**
     * 设置字符集
     * @param string $charset
     * @return Conf
     */
    public function setCharset($charset)
    {
        is_string($charset) && $this->charset = $charset;
        return $this;
    }
    /**
     * 开启混淆曲线
     * @param bool $useCurve
     * @return Conf
     */
    public function setUseCurve($useCurve = true)
    {
        is_bool($useCurve) && $this->useCurve = $useCurve;
        return $this;
    }
    /**
     * 开启噪点生成
     * @param bool $useNoise
     * @return Conf
     */
    public function setUseNoise($useNoise = true)
    {
        is_bool($useNoise) && $this->useNoise = $useNoise;
        return $this;
    }
    /**
     * 使用自定义字体
     * @param string $useFont
     * @return Conf
     */
    public function setUseFont($useFont)
    {
        is_string($useFont) && $this->useFont = $useFont;
        return $this;
    }
    /**
     * 设置文字颜色
     * @param array|string $fontColor
     * @return Conf
     */
    public function setFontColor($fontColor)
    {
        if (is_string($fontColor)) $this->fontColor = $this->HEXToRGB($fontColor);
        if (is_array($fontColor)) $this->fontColor = $fontColor;
        return $this;
    }
    /**
     * 设置背景颜色
     * @param null $backColor
     * @return Conf
     */
    public function setBackColor($backColor)
    {
        if (is_string($backColor)) $this->backColor = $this->HEXToRGB($backColor);
        if (is_array($backColor)) $this->backColor = $backColor;
        return $this;
    }
    /**
     * 设置图片宽度
     * @param int|string $imageL
     * @return Conf
     */
    public function setImageWidth($imageL)
    {
        $this->imageL = intval($imageL);
        return $this;
    }
    /**
     * 设置图片高度
     * @param null $imageH
     * @return Conf
     */
    public function setImageHeight($imageH)
    {
        $this->imageH = intval($imageH);
        return $this;
    }
    /**
     * 设置字体集
     * @param array|string $fonts
     * @return Conf
     */
    public function setFonts($fonts)
    {
        if (is\_string($fonts)) array\_push($this->fonts, $fonts);
        if (is_array($fonts) && !empty($fonts)) {
            if (empty($this->fonts)) {
                $this->fonts = $fonts;
            } else {
                array_merge($this->fonts, $fonts);
            }
        }
        return $this;
    }
    /**
     * 设置字体尺寸
     * @param int $fontSize
     * @return Conf
     */
    public function setFontSize($fontSize)
    {
        $this->fontSize = intval($fontSize);
        return $this;
    }
    /**
     * 设置验证码长度
     * @param int $length
     * @return Conf
     */
    public function setLength($length)
    {
        $this->length = intval($length);
        return $this;
    }
    /**
     * 获取配置值
     * @param $name
     * @author : evalor <master@evalor.cn>
     * @return mixed
     */
    public function __get($name)
    {
        return $this->$name;
    }
    /**
     * 十六进制转RGB
     * @param $hexColor
     * @author : evalor <master@evalor.cn>
     * @return array
     */
    function HEXToRGB($hexColor)
    {
        $color = str_replace('#', '', $hexColor);
        if (strlen($color) > 3) {
            $rgb = array(
                hexdec(substr($color, 0, 2)),
                hexdec(substr($color, 2, 2)),
                hexdec(substr($color, 4, 2))
            );
        } else {
            $color = $hexColor;
            $r = substr($color, 0, 1) . substr($color, 0, 1);
            $g = substr($color, 1, 1) . substr($color, 1, 1);
            $b = substr($color, 2, 1) . substr($color, 2, 1);
            $rgb = array(
                hexdec($r),
                hexdec($g),
                hexdec($b)
            );
        }
        return $rgb;
    }}

关于result输出结果

/**
     * 获取验证码图片
     * @author : evalor <master@evalor.cn>
     * @return mixed
     */
    function getImageByte()
    {
        return $this->CaptchaByte;
    }
    /**
     * 返回图片Base64字符串
     * @author : evalor <master@evalor.cn>
     * @return string
     */
    function getImageBase64()
    {
        $base64Data = base64_encode($this->CaptchaByte);
        $Mime = $this->CaptchaMime;
        return "data:{$Mime};base64,{$base64Data}";
    }
    /**
     * 获取验证码内容
     * @author : evalor <master@evalor.cn>
     * @return mixed
     */
    function getImageCode()
    {
        return $this->CaptchaCode;
    }
    /**
     * 获取Mime信息
     * @author : evalor <master@evalor.cn>
     */
    function getImageMime()
    {
        return $this->CaptchaMime;
    }
    /**
     * 获取验证码文件路径
     * @author: eValor < master@evalor.cn >
     */
    function getImageFile()
    {
        return $this->CaptchaFile;
    }

本文转载自 www.easyswoole.com 官方文档

目录
相关文章
|
10月前
|
PHP
PHP实现图片登录验证码的解决方案
PHP实现图片登录验证码的解决方案
75 0
|
前端开发 PHP
PHP短信验证码
这篇文章主要为大家详细介绍了PHP发送短信接入验证码的实现流程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
PHP短信验证码
|
人工智能 安全 前端开发
PHP验证码
验证码(CAPTCHA)是“Completely Automated Public Turing test to tell Computers and Humans Apart”(全自动区分计算机和人类的图灵测试)的缩写,是一种区分用户是计算机还是人的公共全自动程序。
PHP验证码
PHP登录验证码
本文实例讲述了PHP登录验证功能。分享给大家供大家参考,具体如下:
PHP登录验证码
|
SQL API 数据库
easyswoole快速实现一个网站的api接口程序
easyswoole快速实现一个网站的api接口程序
98 0
easyswoole快速实现一个网站的api接口程序
|
人工智能 前端开发 安全
PHP制作简单的验证码验证
 验证码(CAPTCHA)是“Completely Automated Public Turing test to tell Computers and Humans Apart”(全自动区分计算机和人类的图灵测试)的缩写,是一种区分用户是计算机还是人的公共全自动程序。
93 0
PHP制作简单的验证码验证
|
PHP
laravel怎么加入验证码功能
laravel怎么加入验证码功能
128 0
laravel怎么加入验证码功能
|
PHP
PHP生成验证码图片
PHP生成验证码图片
130 0
PHP生成验证码图片
|
测试技术
Confluence 6 配置验证码(Captcha)来防止垃圾
如果你的 Confluence 站点是对公众开放的(允许匿名用户使用,添加评论,创建页面等),你可能会发现你的站点会被自动创建很多垃圾页面,评论或者其他垃圾内容。
1082 0
|
PHP
PHP验证码图片生成
<?php // PHP验证码图片生成 session_start(); $image = imagecreatetruecolor(100,30); $bgcolor = imagecolorallocate($image,255,255,255);//#FFFFFFFFFFFF imagefi.
1555 0