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 相关文章推荐
PHP 采集程序原理分析篇
Mar 05 PHP
PHP 数组基础知识小结
Aug 20 PHP
一个基于PDO的数据库操作类(新) 一个PDO事务实例
Jul 03 PHP
ThinkPHP3.1新特性之多层MVC的支持
Jun 19 PHP
PHP+jQuery 注册模块的改进(三):更新到Smarty3.1
Oct 14 PHP
浅谈php冒泡排序
Dec 30 PHP
PHP使用Pear发送邮件(Windows环境)
Jan 05 PHP
php通过文件头判断格式的方法
May 28 PHP
PHP实现更改hosts文件的方法示例
Aug 08 PHP
基于Laravel实现的用户动态模块开发
Sep 21 PHP
Yii框架的布局文件实例分析
Sep 04 PHP
Laravel框架实现抢红包功能示例
Oct 31 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
解决phpmyadmin中文乱码问题。。。
2007/01/18 PHP
用PHPdig打造属于你自己的Google[图文教程]
2007/02/14 PHP
thinkphp判断访客为手机端或PC端的方法
2014/11/24 PHP
php生成mysql的数据字典
2016/07/07 PHP
PHP页面静态化――纯静态与伪静态用法详解
2020/06/05 PHP
JavaScript控制按钮可用或不可用的方法
2015/04/03 Javascript
node.js 使用ejs模板引擎时后缀换成.html
2015/04/22 Javascript
js+flash实现的5图变换效果广告代码(附演示与demo源码下载)
2016/04/01 Javascript
js事件源window.event.srcElement兼容性写法(详解)
2016/11/25 Javascript
jQuery编写设置和获取颜色的插件
2017/01/09 Javascript
JS实现本地存储信息的方法(基于localStorage与userData)
2017/02/18 Javascript
浅谈express 中间件机制及实现原理
2017/08/31 Javascript
Web开发使用Angular实现用户密码强度判别的方法
2017/09/27 Javascript
jQuery实现的点击按钮改变样式功能示例
2018/07/21 jQuery
微信小程序 腾讯地图显示偏差问题解决
2019/07/27 Javascript
关于layui导航栏不展示下拉列表的解决方法
2019/09/25 Javascript
微信小程序利用for循环解决内容变更问题
2020/03/05 Javascript
举例简单讲解Python中的数据存储模块shelve的用法
2016/03/03 Python
python实现逻辑回归的方法示例
2017/05/02 Python
Python实现Pig Latin小游戏实例代码
2018/02/02 Python
详解Django解决ajax跨域访问问题
2018/08/24 Python
python实现文件的备份流程详解
2019/06/18 Python
Python叠加两幅栅格图像的实现方法
2019/07/05 Python
python画图——实现在图上标注上具体数值的方法
2019/07/08 Python
python3.5 cv2 获取视频特定帧生成jpg图片
2019/08/28 Python
初次部署django+gunicorn+nginx的方法步骤
2019/09/11 Python
Python 实现一个计时器
2020/07/28 Python
使用phonegap播放音频的实现方法
2017/03/31 HTML / CSS
纽约现代艺术博物馆商店:MoMA STORE(室内家具和杂货商品)
2016/08/02 全球购物
Boutique 1美国:阿联酋奢侈时尚零售商
2017/10/16 全球购物
abstract 可以和 virtual 一起使用吗?可以和 override 一起使用吗?
2012/10/15 面试题
和谐家庭演讲稿
2014/05/24 职场文书
微电影大赛策划方案
2014/06/05 职场文书
小学一年级数学教学计划
2015/01/20 职场文书
幼儿园开学家长寄语(2015秋季)
2015/05/27 职场文书
学雷锋活动简报
2015/07/20 职场文书