thinkphp5.0.24验证码

这里使用composer安装验证码类
检查vendor下topthink是否有:think-captcha
如果没有, 进入CMD命令行界面使用composer下载:

为了提升速度,建议使用国内镜像站点。

composer config -g repo.packagist composer https://packagist.phpcomposer.com

cd到web目录下执行以下命令:
tp5.0的使用1.0下的扩展包,这里使用1.0.8版本

composer require topthink/think-captcha=1.0.8

然后再进入配置文件配置验证码的规则

//验证码
    'captcha' =>[
        //验证码的字符集
        'codeSet' => '123456798',
        //设置字体大小
        'fontSize' => 18,
        //添加混淆曲线
        'useCurve' => true,
        //设置图片宽高
        'imagew' => 150,
        'imageH' => 35,
        //位数
        'length' => 4,
        //验证成功重置
        'reset' => true,
    ],

在vendor\topthink\think-captcha\src\helper.php;找到captcha_img()方法
将里面内容替换成

$js_src = "this.src='".captcha_src()."'";
    return '点击更新验证码';

控制器写入方法

public function index()
{
    return $this->fetch();
}
//验证
public function captcha(){
    	if(request()->isPost()){
            $data = input('post.');
            if(!captcha_check($data['verifyCode'])) {
                // 校验失败
                $this->error('验证码不正确');
            }else{
            	$this->success('验证码正确');
            }
		}
    }

index视图

{:captcha_img()}

提示:如果php版本是7以上的可能会不显示
需要在配置文件php.ini把extension=php_gd2.dll,前面分号去掉,如果没有就加上这句。

你可能感兴趣的:(PHP,thinkphp使用验证码)