TP6验证码,TP3.2验证码,PHP验证码

                                                                                **TP6验证码,TP3.2验证码,PHP验证码*

1.安装扩张包
根目录执行命令: composer require topthink/think-captcha

2.打开/config/captcha.php文件 自定义验证规则

          <?php
// +----------------------------------------------------------------------
// | Captcha配置文件
// +----------------------------------------------------------------------

return [
    //验证码位数
    'length'   => 5,
    // 验证码字符集合
    'codeSet'  => '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY',
    // 验证码过期时间
    'expire'   => 1800,
    // 是否使用中文验证码
    'useZh'    => false,
    // 是否使用算术验证码
    'math'     => false,
    // 是否使用背景图
    'useImgBg' => false,
    //验证码字符大小
    'fontSize' => 25,
    // 是否使用混淆曲线
    'useCurve' => true,
    //是否添加杂点
    'useNoise' => true,
    // 验证码字体 不设置则随机
    'fontttf'  => '',
    //背景颜色
    'bg'       => [243, 251, 254],
    // 验证码图片高度
    'imageH'   => 0,
    // 验证码图片宽度
    'imageW'   => 0,
    // 添加自定义的验证码设置
     'verify' => [
         'codeSet' => '2345678019',
         'expire' => 18000,            // 验证码过期时间(s)
         'useImgBg' => false,           // 使用背景图片
         'fontSize' => 18,              // 验证码字体大小(px)
         'useCurve' => false,            // 是否画混淆曲线
         'useNoise' => false,            // 是否添加杂点
         'useZh' => false,           // 使用中文验证码
         'length' => 4,               // 验证码位数
         'fontttf' => '',              // 验证码字体,不设置随机获取
         'bg' => array(243, 251, 254),  // 背景颜色
         'reset' => true,           // 验证成功后是否重置
    ],
];

3.打开/app/middleware.php 将session打开
// Session初始化
\think\middleware\SessionInit::class
4.前端展示

   <input class="input" type="text" maxlength="4" ref="code" v-model="code" placeholder="输入你的验证码  @input="codeInp" @keyup.enter="checkForm">
          <span class="xian"></span>
          <img class="newcode" id="newcode" src="{:captcha_src('verify')}" alt="captcha" οnclick="this.src=this.src+'?'" style="height: 140%;float: right"/> 

4.后台验证

  if(!captcha_check($content['code'])){
        $res['status'] = 0;
        $res['msg'] = '请输入正确验证码';
        return json($res);
    }   
tp3.2验证码
use Think\Verify;
 //显示图文验证码
 public function verify()
  {
      $config = array(
          'codeSet' => '23456890', // 验证码字符集合
          'expire' => 18000,            // 验证码过期时间(s)
          'useZh' => false,           // 使用中文验证码
          'useImgBg' => false,           // 使用背景图片
          'fontSize' => 16,              // 验证码字体大小(px)
          'useCurve' => false,            // 是否画混淆曲线
          'useNoise' => false,            // 是否添加杂点
          'imageH' => 40,               // 验证码图片高度
          'imageW' => 105,               // 验证码图片宽度
          'length' => 4,               // 验证码位数
          'fontttf' => '',              // 验证码字体,不设置随机获取
          'bg' => array(243, 251, 254),  // 背景颜色
          'reset' => true,           // 验证成功后是否重置
      );
      $Verify = new Verify($config);
      $Verify->entry();
  }
         
  //判断验证码是否输入正确
   $ver = new Verify();
   $vers = $ver->check($_POST['captcha']);
   if ($vers != 1) {
    $res['status'] = 0;
     $res['msg'] = '验证码不正确';
     $this->ajaxReturn($res);
  }
        
 //前端
    <div class="form-input">
               <label class="icon code"></label>
               <input class="input" type="text" maxlength="4" ref="code" v-model="code" placeholder="输入你的验证码"
                      @input="codeInp" @keyup.enter="checkForm">
               <span class="xian"></span>
               <img class="newcode" id="newcode" src="{:U('Login/verify')}" id="newcode"
                    onclick="this.src='__CONTROLLER__/verify/'+Math.random()" style="height: 140%;float: right"/>
    </div>
    .
//php生成验证码
 $font = 3; //字体宽度
 $arr = array_merge(range('a','z'),range('A','Z'),range(1,9)); //定义验证码范围
 shuffle($arr); //打乱数组顺序
 $code = ''; //验证码
 for($i=0;$i<4;$i++){
     $code .= $arr[mt_rand(0,count($arr) - 1)];
 }
 session_start(); //开启session
 $_SESSION['user_code'] = $code;
 $img = imagecreatetruecolor($width=100,$height=100); //生成画布
 $color = imagecolorallocate($img,0,0,0); //设置颜色
 $font_w = ($width - $font * 4)/2;  //字体横向作表 图片宽度减去字体总宽度除以2 居中
 imagestring($img,30,$font_w,$font_h = 100,$code,$color); //图像,字体大小,横作表,纵作表,验证码,颜色
 //输出
 header("Content-type:image/png");
 imagepng($img);
 imagedestroy($img); //销毁图像句柄
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值