thinkphp6.0使用内置的验证码

一、引入模块

使用composer安装

composer require topthink/think-captcha

二、前端引用

在验证码模块的helper.php文件中,修改验证码样式:

function captcha_img($width='',$height='',$id = '', $domid = ''): string
{
    $src = captcha_src($id);
  
    $domid = empty($domid) ? $domid : "id='" . $domid . "'";

    $style="";

    if($height&&$width){
        $style='width="'.$width.'";hight="'.$height.'"';
    }

    return "<img src='{$src}'  $style  alt='captcha' " . $domid . " οnclick='this.src=\"{$src}?\"+Math.random();' />";
}

在页面二维码图片处写入:

<div class="validateImg" >{:captcha_img(144,50)}</div>

三、后端验证

首先开启session,在全局中间件中,设置:

'think\middleware\SessionInit'

然后在php代码中验证,代码为:

        //校验验证码是否正确
        if(!captcha_check($captcha)){
            $res=[
                'status'=>400,
                'msg'=>'验证码不正确!'
            ];    
            return json($res);
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值