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 相关文章推荐
MySQL连接数超过限制的解决方法
Jul 17 PHP
php 按指定元素值去除数组元素的实现方法
Nov 04 PHP
MySQL的FIND_IN_SET函数使用方法分享
Mar 27 PHP
用PHP实现浏览器点击下载TXT文档的方法详解
Jun 02 PHP
采用header定义为文件然后readfile下载(隐藏下载地址)
Jan 31 PHP
PHP+redis实现添加处理投票的方法
Nov 14 PHP
PHP基于单例模式实现的mysql类
Jan 09 PHP
win10环境PHP 7 安装配置【教程】
May 09 PHP
php for 循环使用的简单实例
Jun 02 PHP
php mysql like 实现多关键词搜索的方法
Oct 29 PHP
php事件驱动化设计详解
Nov 10 PHP
PHP基于GD2函数库实现验证码功能示例
Jan 27 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
用PHP创建PDF中文文档
2006/10/09 PHP
php daodb插入、更新与删除数据
2009/03/19 PHP
yii框架通过控制台命令创建定时任务示例
2014/04/30 PHP
19个Android常用工具类汇总
2014/12/30 PHP
php判断linux下程序问题实例
2015/07/09 PHP
PHP版本常用的排序算法汇总
2015/12/20 PHP
php用正则判断是否为数字的方法
2016/03/25 PHP
PHP采用超长(超大)数字运算防止数字以科学计数法显示的方法
2016/04/01 PHP
ThinkPHP实现图片上传操作的方法详解
2017/05/08 PHP
angularjs 处理多个异步请求方法汇总
2015/01/06 Javascript
js replace(a,b)之替换字符串中所有指定字符的方法
2016/08/17 Javascript
利用JS做网页特效_大图轮播(实例讲解)
2017/08/09 Javascript
Webpack实战加载SVG的方法
2017/12/26 Javascript
解析Vue.js中的组件
2018/02/02 Javascript
jQuery实现每隔一段时间自动更换样式的方法分析
2018/05/03 jQuery
Vue源码解读之Component组件注册的实现
2018/08/24 Javascript
JavaScript变量提升和严格模式实例分析
2019/01/27 Javascript
在Vue项目中引入JQuery-ui插件的讲解
2019/01/27 jQuery
Vue实现类似Spring官网图片滑动效果方法
2019/03/01 Javascript
详解NodeJs项目 CentOs linux服务器线上部署
2019/09/16 NodeJs
Vue中使用matomo进行访问流量统计的实现
2019/11/05 Javascript
[55:32]2018DOTA2亚洲邀请赛 4.4 淘汰赛 EG vs LGD 第二场
2018/04/05 DOTA
python字典DICT类型合并详解
2017/08/17 Python
python顺序的读取文件夹下名称有序的文件方法
2018/07/11 Python
win10下python3.5.2和tensorflow安装环境搭建教程
2018/09/19 Python
pycharm 激活码及使用方式的详细教程
2020/05/12 Python
sklearn线性逻辑回归和非线性逻辑回归的实现
2020/06/09 Python
收集的7个CSS3代码生成工具
2010/04/17 HTML / CSS
CSS3中HSL和HSLA的简单使用示例
2015/07/14 HTML / CSS
美国礼品卡商城: Gift Card Mall
2017/08/25 全球购物
英国在线定制百叶窗网站:Swift Direct Blinds
2020/02/25 全球购物
荷兰浴室和卫浴网上商店:Badkamerxxl.nl
2020/10/06 全球购物
Java面试题:说出如下代码的执行结果
2015/10/30 面试题
毕业生银行实习自我鉴定
2014/10/14 职场文书
Nginx配置https的实现
2021/11/27 Servers
浅谈MySQL中的六种日志
2022/03/23 MySQL