侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Thinkphp3.2实用篇之计算型验证码示例

ThinkPHP  /  管理员 发布于 8年前   278

是不是觉得普通的验证码已经没办法满足,接下来介绍如何将tp现有的验证码改为计算型验证码:

首先找到:ThinkPHP\Library\Think\Verify.class.php

在其中加入以下代码:

public function entry_add($id = '') {    $this->length='3';    // 图片宽(px)    $this->imageW || $this->imageW = $this->length*$this->fontSize*1.5 + $this->length*$this->fontSize/2;     // 图片高(px)    $this->imageH || $this->imageH = $this->fontSize * 2.5;    // 建立一幅 $this->imageW x $this->imageH 的图像    $this->_image = imagecreate($this->imageW, $this->imageH);     // 设置背景       imagecolorallocate($this->_image, $this->bg[0], $this->bg[1], $this->bg[2]);     // 验证码字体随机颜色    $this->_color = imagecolorallocate($this->_image, mt_rand(1,150), mt_rand(1,150), mt_rand(1,150));    // 验证码使用随机字体    $ttfPath = dirname(__FILE__) . '/Verify/' . ($this->useZh ? 'zhttfs' : 'ttfs') . '/';    if(empty($this->fontttf)){      $dir = dir($ttfPath);      $ttfs = array();          while (false !== ($file = $dir->read())) {        if($file[0] != '.' && substr($file, -4) == '.ttf') {          $ttfs[] = $file;        }      }      $dir->close();      $this->fontttf = $ttfs[array_rand($ttfs)];    }     $this->fontttf = $ttfPath . $this->fontttf;        if($this->useImgBg) {      $this->_background();    }        if ($this->useNoise) {      // 绘杂点      $this->_writeNoise();    }    if ($this->useCurve) {      // 绘干扰线      $this->_writeCurve();    }        // 绘验证码    $code = array(); // 验证码    $symbol=array('+','-');    $codeNX = 0; // 验证码第N个字符的左边距    $now_symbol=$symbol[rand(0,1)];    for ($i = 0; $i<$this->length; $i++) {      if($i==1){        $code[$i] = $now_symbol;        $codeNX += mt_rand($this->fontSize*1.2, $this->fontSize*1.6);        imagettftext($this->_image, $this->fontSize,0, $codeNX, $this->fontSize*1.6, $this->_color, $ttfPath.'2.ttf', $code[$i]);      }      else{        $code[$i] = $this->codeSet[mt_rand(0, strlen($this->codeSet)-1)];        $codeNX += mt_rand($this->fontSize*1.2, $this->fontSize*1.6);        imagettftext($this->_image, $this->fontSize, mt_rand(-40, 40), $codeNX, $this->fontSize*1.6, $this->_color, $this->fontttf, $code[$i]);      }     }        // 保存验证码    $key    =  $this->authcode($this->seKey);    $str=implode('', $code);    eval("\$re=$str;");    $code    =  $this->authcode($re);    $secode   =  array();    $secode['verify_code'] = $code; // 把校验码保存到session    $secode['verify_time'] = NOW_TIME; // 验证码创建时间    session($key.$id, $secode);                header('Cache-Control: private, max-age=0, no-store, no-cache, must-revalidate');    header('Cache-Control: post-check=0, pre-check=0', false);        header('Pragma: no-cache');    header("content-type: image/png");    // 输出图像    imagepng($this->_image);    imagedestroy($this->_image);  }
public function check_add($code, $id = '') {    $key = $this->authcode($this->seKey).$id;    // 验证码不能为空    $secode = session($key);    if($code===false || empty($secode)) {      return false;    }    //验证码是否是数字    if(!is_numeric($code)) {      return false;    }    // session 过期    if(NOW_TIME - $secode['verify_time'] > $this->expire) {      session($key, null);      return false;    }    if($this->authcode($code) == $secode['verify_code']) {      $this->reset && session($key, null);      return true;    }    return false;  }

生成方法:

Public function verify(){    import('ORG.Util.Verify');    $Verify = new Verify();    $Verify->useNoise = true;    $Verify->codeSet = '0123456789';    $Verify->useCurve = false;    $Verify->entry_add();  }

验证方法:

 if (!check_verify($verify,'','add')) {      $this->error('验证码错误!');      return;    }

 调用的公共方法:

 // 检测输入的验证码是否正确,$code为用户输入的验证码字符串function check_verify($code, $id = '',$type=''){  import('ORG.Util.Verify');  $verify = new Verify();  if($type='add'){    return $verify->check_add($code, $id);  }  else{    return $verify->check($code, $id);  }}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

您可能感兴趣的文章:

  • thinkphp5.1验证码及验证码验证功能的实现详解
  • tp5(thinkPHP5框架)captcha验证码配置及验证操作示例
  • ThinkPHP5.0框架验证码功能实现方法【基于第三方扩展包】
  • thinkPHP5.0框架验证码调用及点击图片刷新简单实现方法
  • thinkPHP实现的验证码登录功能示例
  • thinkphp3.2实现在线留言提交验证码功能
  • ThinkPHP实现生成和校验验证码功能
  • thinkPHP中验证码的简单实现方法
  • ThinkPHP5&5.1实现验证码的生成、使用及点击刷新功能示例


  • 上一条:
    ThinkPHP+EasyUI之ComboTree中的会计科目树形菜单实现方法
    下一条:
    解决nginx不支持thinkphp中pathinfo的问题
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • thinkphp + mongodb项目中数据加载慢问题分析及解决(0个评论)
    • thinkphp6框架中封装redis操作类(0个评论)
    • thinkphp6框架中实现定时任务功能流程步骤(0个评论)
    • Thinkphp5.1框架中实现Session+Redis会话共享流程步骤(0个评论)
    • TP5框架版本5.0.10安全漏洞根据官方补丁修复,也是本站安全漏洞修复(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-12
    • 2018-01
    • 2018-02
    • 2020-03
    • 2021-07
    • 2021-12
    • 2022-05
    • 2022-06
    • 2022-09
    • 2023-01
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客