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


Posted in PHP onFebruary 09, 2017

是不是觉得普通的验证码已经没办法满足,接下来介绍如何将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);
  }
}

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

PHP 相关文章推荐
56.com视频采集接口程序(PHP)
Sep 22 PHP
php下用cookie统计用户访问网页次数的代码
May 09 PHP
php实现查看邮件是否已被阅读的方法
Dec 03 PHP
那些年我们错过的魔术方法(Magic Methods)
Jan 14 PHP
PHP之autoload运行机制实例分析
Aug 28 PHP
PHP+Mysql基于事务处理实现转账功能的方法
Jul 08 PHP
php创建无限级树型菜单
Nov 05 PHP
利用php_imagick实现复古效果的方法
Oct 18 PHP
PHP通过文件保存和更新信息的方法分析
Sep 12 PHP
php操作redis数据库常见方法实例总结
Feb 20 PHP
Thinkphp 框架扩展之应用模式实现方法分析
Apr 27 PHP
php如何获取Http请求
Apr 30 PHP
PHP 验证身份证是否合法的函数
Feb 09 #PHP
如何打开php的gd2库
Feb 09 #PHP
利用PHP访问带有密码的Redis方法示例
Feb 09 #PHP
PHP获取表单数据与HTML嵌入PHP脚本的实现
Feb 09 #PHP
使用php实现网站验证码功能【推荐】
Feb 09 #PHP
form表单传递数组数据、php脚本接收的实例
Feb 09 #PHP
PHP设置Cookie的HTTPONLY属性方法
Feb 09 #PHP
You might like
第五节--克隆
2006/11/16 PHP
php对gzip文件或者字符串解压实例参考
2008/07/25 PHP
PHP下打开phpMyAdmin出现403错误的问题解决方法
2013/05/23 PHP
用php+ajax新建流程(请假、进货、出货等)
2017/06/11 PHP
Thinkphp 框架配置操作之配置加载与读取配置实例分析
2020/05/15 PHP
js用正则表达式来验证表单(比较齐全的资源)
2013/11/17 Javascript
Js获取下拉框选定项的值和文本的实现代码
2014/02/26 Javascript
使用jQuery不判断浏览器高度解决iframe自适应高度问题
2014/12/16 Javascript
javascript转换日期字符串为Date日期对象的方法
2015/02/13 Javascript
使用jQuery给input标签设置默认值
2016/06/20 Javascript
详解nodejs微信公众号开发——2.自动回复
2017/04/10 NodeJs
关于 angularJS的一些用法
2017/11/29 Javascript
js中getter和setter用法实例分析
2018/08/14 Javascript
在vue中v-bind使用三目运算符绑定class的实例
2018/09/29 Javascript
ES6中的迭代器、Generator函数及Generator函数的异步操作方法
2019/05/12 Javascript
node解析修改nginx配置文件操作实例分析
2019/11/06 Javascript
jquery实现烟花效果(面向对象)
2020/03/10 jQuery
使用python编写android截屏脚本双击运行即可
2014/07/21 Python
Python实现命令行通讯录实例教程
2016/08/18 Python
python抽取指定url页面的title方法
2018/05/11 Python
解决django前后端分离csrf验证的问题
2019/02/03 Python
Python实现字符型图片验证码识别完整过程详解
2019/05/10 Python
python 如何停止一个死循环的线程
2020/11/24 Python
Urban Outfitters美国官网:美国生活方式品牌
2016/08/26 全球购物
跑鞋、网球鞋、网球拍、服装及装备:Holabird Sports
2016/09/19 全球购物
美国Max仓库:Max Warehouse
2020/05/31 全球购物
畜牧兽医本科生个人的自我评价
2013/10/11 职场文书
营业员实习自我鉴定
2013/12/07 职场文书
网站创业计划书
2014/04/30 职场文书
踏青活动策划方案
2014/08/19 职场文书
课外访万家心得体会
2014/09/03 职场文书
婚礼庆典答谢词
2015/01/20 职场文书
史上最牛的辞职信
2015/02/28 职场文书
2015年食品安全工作总结
2015/05/15 职场文书
中小学生安全教育观后感
2015/06/17 职场文书
ORACLE数据库应用开发的三十个注意事项
2021/06/07 Oracle