服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - PHP教程 - Thinkphp3.2实用篇之计算型验证码示例

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

2021-04-21 16:23phper君 PHP教程

本篇文章主要介绍了Thinkphp3.2实用篇之计算型验证码示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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

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

在其中加入以下代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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);
  }
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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;
  }

生成方法:

?
1
2
3
4
5
6
7
8
Public function verify(){
    import('ORG.Util.Verify');
    $Verify = new Verify();
    $Verify->useNoise = true;
    $Verify->codeSet = '0123456789';
    $Verify->useCurve = false;
    $Verify->entry_add();
  }

验证方法:

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

 调用的公共方法:

?
1
2
3
4
5
6
7
8
9
10
11
// 检测输入的验证码是否正确,$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);
  }
}

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

原文链接:http://www.cnblogs.com/phperyin/archive/2017/02/09/6382305.html

延伸 · 阅读

精彩推荐
  • PHP教程php连接oracle数据库的核心步骤

    php连接oracle数据库的核心步骤

    这篇文章主要介绍了php连接oracle数据库的核心步骤,简要分析了php安装Oracle扩展设置及连接测试代码,非常简单易懂,需要的朋友可以参考下...

    CallMeStone11562021-01-23
  • PHP教程基于php无限分类的深入理解

    基于php无限分类的深入理解

    本篇文章是对php无限分类就行了详细的分析介绍,需要的朋友参考下 ...

    PHP教程网3132020-03-27
  • PHP教程浅析PHP开发规范

    浅析PHP开发规范

    本篇文章是小编为大家整理的关于PHP开发规范的相关内容,适合PHP初学者,一起参考下吧。 ...

    laozhang1532019-10-23
  • PHP教程PHP临时文件的安全性分析

    PHP临时文件的安全性分析

    这篇文章主要介绍了PHP临时文件的安全性分析,对于临时文件的安全性及防范措施作出了比较详细的阐述,需要的朋友可以参考下 ...

    PHP教程网2802020-07-16
  • PHP教程php删除一个路径下的所有文件夹和文件的方法

    php删除一个路径下的所有文件夹和文件的方法

    php遍历一个文件夹内的所有文件和文件夹,并删除所有文件夹和子文件夹下的所有文件的代码,通过递归方式实现达到清空一个目录的效果。本文给大家分...

    切切歆语2082019-10-22
  • PHP教程100行PHP代码实现socks5代理服务器

    100行PHP代码实现socks5代理服务器

    这个例子说了in the real world, do something here to ensure clients not running are destroyed 但是,如何把不再运行的连接销毁却没有讲。恩。我试了把$clients放到一个类里...

    秣马儿10512021-01-13
  • PHP教程Yii视图CGridView列表用法实例分析

    Yii视图CGridView列表用法实例分析

    这篇文章主要介绍了Yii视图CGridView列表用法,结合实例形式分析了CGridView列表的视图及功能实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    dreamzml9182021-02-04
  • PHP教程php+xml实现在线英文词典之添加词条的方法

    php+xml实现在线英文词典之添加词条的方法

    这篇文章主要介绍了php+xml实现在线英文词典之添加词条的方法,接着上一篇的通过英文查询汉字进一步完善了词条的添加功能,具有一定参考借鉴价值,需要的...

    PHP教程网4692020-08-31