gitzwt/think-captcha-1.1

thinkphp5.0 验证码类库,支持缓存存储,方便API调用

v1.1 2018-11-15 05:27 UTC

This package is auto-updated.

Last update: 2024-09-15 18:46:09 UTC


README

thinkphp5.0 验证码类库(修改版)

修改部分,新增缓存缓存存储验证码信息(便于API接口调用使用)

安装

composer require gitzwt/think-captcha-1.1

/**
     * 获取图片验证码(接口调用示例,type为cache时)
     * @return \think\response\Json
     */
    public function getCaptcha()
    {
        $captcha_id = Strs::randString(4, 1) . date('s'); //生成随机id
        //接口构建类的返回
        return $this->buildSuccess([
            'CaptchaId' => (String)$captcha_id, //captcha_id
            'CaptchaSrc' => $this->request->domain() . captcha_src($captcha_id) //验证码url
        ]);
    }

##使用

extra子目录内配置 captcha.php

###模板里输出验证码

<div>{:captcha_img()}</div>

或者

<div><img src="{:captcha_src()}" alt="captcha" /></div>

上面两种的最终效果是一样的

控制器里验证

使用TP5的内置验证功能即可

$this->validate($data,[
    'captcha|验证码'=>'required|captcha'
]);

或者手动验证

if(!captcha_check($captcha)){
 //验证失败
};

具体使用参照tp5.0完全开发手册验证码部分