PHP验证码类ValidateCode解析

 更新时间:2017年1月15日 22:11  点击:1719

PHP解析验证码类

1.开始

在网上看到使用PHP写的ValidateCode生成验证码类,感觉不错,特拿来分析学习一下。

2.类图

 

3.验证码类部分代码

3.1  定义变量

  //随机因子
  private $charset = 'abcdefghjkmnprstuvwxyzABCDEFGJKMNPRSTUVWXYZ23456789';
  private $code;
  private $codeLen = 4;

  private $width = 130;
  private $heigh = 50;
  private $img;//图像

  private $font;//字体
  private $fontsize = 20;

$charset 是随机因子,这里是去掉了几个不容易区分的字符,如字母"i,l,o,q",数字"0,1"。有必要可以加入一些中文或其他字符或算式等。

$codeLen表示验证码长度,常见4位。

3.2构造函数,设置验证码字体,生成一个真彩色图像img

public function __construct()
  {
    $this->font = ROOT_PATH.'/font/Chowderhead.ttf';
    $this->img = imagecreatetruecolor($this->width, $this->heigh);
  }

3.3从随机因子中随机抽取4个字符,作为$code验证码.

//生成随机码
  private function createCode()
  {
    $_len = strlen($this->charset) - 1;
    for ($i = 0; $i < $this->codeLen; $i++) {
      $this->code .= $this->charset[mt_rand(0, $_len)];
    }
  }

3.4生成验证码背景色.

//生成背景
  private function createBg()
  {
$color = imagecolorallocate($this->img, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255));
    imagefilledrectangle($this->img, 0, $this->heigh, $this->width, 0, $color);

  }

其中mt_rand(157, 255),目的是随机取比较浅的颜色。

3.5在图像上生成文字.

//生成文字
  private function createFont()
  {
    $_x = $this->width / $this->codeLen;
    $_y = $this->heigh / 2;
    for ($i = 0; $i < $this->codeLen; $i++) {
      $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
      imagettftext($this->img, $this->fontsize, mt_rand(-30, 30), $_x * $i + mt_rand(3, 5), $_y + mt_rand(2, 4), $color, $this->font, $this->code[$i]);
    }
  }

在图像上生成验证码文字,主要考虑文字在图像上的位置和每一个文字颜色。

控制第n个文字的x轴位置 =  (图像宽度 / 验证码长度) * (n-1)  +  随机的偏移数;  其中n = {d1....n}

控制第n个文字的y轴位置 =  图像高度 / 2 +  随机的偏移数;

mt_rand(0, 156) 随机取文字颜色,0-156目的是取比较深的颜色。

mt_rand(-30, 30) 随机的文字旋转。

3.6在图像上生成线条和雪花

//生成线条,雪花
  private function createLine()
  {
    for ($i = 0; $i < 15; $i++) {
      $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
      imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->heigh), mt_rand(0, $this->width), mt_rand(0, $this->heigh), $color);
    }
    for ($i = 0; $i < 150; $i++) {
      $color = imagecolorallocate($this->img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
      imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->heigh), '#', $color);
    }
  }

画线条的时候,取比较深的颜色值,而画雪花的时候取比较淡的颜色值,目的是尽可能的不影响人眼识别验证码,又能干扰自动识别验证码机制。

3.7对外生成验证码图像,供外部调用。

//对外生成
  public function doImg()
  {

    $this->createBg();   //1.创建验证码背景
    $this->createCode();  //2.生成随机码
    $this->createLine();  //3.生成线条和雪花
    $this->createFont();  //4.生成文字
    $this->outPut();    //5.输出验证码图像
  }

3.8完整代码:

<?php

/**
 * Created by PhpStorm.
 * User: andy
 * Date: 16-12-22
 * Time: 下午1:20
 */
class ValidateCode
{
  //随机因子
  private $charset = 'abcdefghjkmnprstuvwxyzABCDEFGJKMNPRSTUVWXYZ23456789';
  private $code;
  private $codeLen = 4;

  private $width = 130;
  private $heigh = 50;
  private $img;//图像

  private $font;//字体
  private $fontsize = 20;


  public function __construct()
  {
    $this->font = ROOT_PATH.'/font/Chowderhead.ttf';
    $this->img = imagecreatetruecolor($this->width, $this->heigh);
  }

  //生成随机码
  private function createCode()
  {
    $_len = strlen($this->charset) - 1;
    for ($i = 0; $i < $this->codeLen; $i++) {
      $this->code .= $this->charset[mt_rand(0, $_len)];
    }
  }

  //生成背景
  private function createBg()
  {

    $color = imagecolorallocate($this->img, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255));
    imagefilledrectangle($this->img, 0, $this->heigh, $this->width, 0, $color);

  }

  //生成文字
  private function createFont()
  {
    $_x = $this->width / $this->codeLen;
    $_y = $this->heigh / 2;
    for ($i = 0; $i < $this->codeLen; $i++) {
      $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
      imagettftext($this->img, $this->fontsize, mt_rand(-30, 30), $_x * $i + mt_rand(3, 5), $_y + mt_rand(2, 4), $color, $this->font, $this->code[$i]);
    }
  }

  //生成线条,雪花
  private function createLine()
  {
    for ($i = 0; $i < 15; $i++) {
      $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
      imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->heigh), mt_rand(0, $this->width), mt_rand(0, $this->heigh), $color);
    }
    for ($i = 0; $i < 150; $i++) {
      $color = imagecolorallocate($this->img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
      imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->heigh), '#', $color);
    }
  }

  //输出图像
  private function outPut()
  {
    header('Content-Type: image/png');
    imagepng($this->img);
    imagedestroy($this->img);
  }

  //对外生成
  public function doImg()
  {

    $this->createBg();   //1.创建验证码背景
    $this->createCode();  //2.生成随机码
    $this->createLine();  //3.生成线条和雪花
    $this->createFont();  //4.生成文字
    $this->outPut();    //5.输出验证码图像
  }

  //获取验证码
  public function getCode()
  {
    return strtolower($this->code);
  }

}

4.测试

测试代码:

<?php
/**
 * Created by PhpStorm.
 * User: andy
 * Date: 16-12-22
 * Time: 下午1:20
 */

define('ROOT_PATH', dirname(__FILE__));
require_once ROOT_PATH.'/includes/ValidateCode.class.php';

$_vc=new ValidateCode();
echo $_vc->doImg();

生成验证码:

5.应用 

 <label>
<img src="../config/code.php" onclick="javascript:this.src='../config/code.php?tm='+Math.random();" />
</label>


上面onclick代码是点击验证码图片,能自动刷新验证码。

code.php:

<?php
/**
 * Created by PhpStorm.
 * User: andy
 * Date: 16-12-22
 * Time: 下午3:43
 */
require substr(dirname(__FILE__),0,-7).'/init.inc.php';

$_vc=new ValidateCode();
echo $_vc->doImg();
$_SESSION['ValidateCode']=$_vc->getCode();

有关应用的完整代码可以从https://git.oschina.net/andywww/myTest 的CMS1.0 文件里下载。

6.小结

在独立测试过程,没发现什么问题;但应用到项目的时候,刚开始发现无法生成验证码图片,网上找了一下,有的说是在outPut()函数中,

在header('Content-Type: image/png'); 这行代码前面增加了一行ob_clean()代码,可以解决验证码这块问题。虽然此方法简单,但这可能会引起其他缓冲数据问题,因为db_clean()功能就是丢弃输出缓冲区中的内容。

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

[!--infotagslink--]

相关文章

  • 源码分析系列之json_encode()如何转化一个对象

    这篇文章主要介绍了源码分析系列之json_encode()如何转化一个对象,对json_encode()感兴趣的同学,可以参考下...2021-04-22
  • php中去除文字内容中所有html代码

    PHP去除html、css样式、js格式的方法很多,但发现,它们基本都有一个弊端:空格往往清除不了 经过不断的研究,最终找到了一个理想的去除html包括空格css样式、js 的PHP函数。...2013-08-02
  • index.php怎么打开?如何打开index.php?

    index.php怎么打开?初学者可能不知道如何打开index.php,不会的同学可以参考一下本篇教程 打开编辑:右键->打开方式->经文本方式打开打开运行:首先你要有个支持运行PH...2017-07-06
  • PHP中func_get_args(),func_get_arg(),func_num_args()的区别

    复制代码 代码如下:<?php function jb51(){ print_r(func_get_args()); echo "<br>"; echo func_get_arg(1); echo "<br>"; echo func_num_args(); } jb51("www","j...2013-10-04
  • PHP编程 SSO详细介绍及简单实例

    这篇文章主要介绍了PHP编程 SSO详细介绍及简单实例的相关资料,这里介绍了三种模式跨子域单点登陆、完全跨单点域登陆、站群共享身份认证,需要的朋友可以参考下...2017-01-25
  • PHP实现创建以太坊钱包转账等功能

    这篇文章主要介绍了PHP实现创建以太坊钱包转账等功能,对以太坊感兴趣的同学,可以参考下...2021-04-20
  • php微信公众账号开发之五个坑(二)

    这篇文章主要为大家详细介绍了php微信公众账号开发之五个坑,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2016-10-02
  • PHP如何通过date() 函数格式化显示时间

    这篇文章主要介绍了PHP如何通过date() 函数格式化显示时间,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-11-13
  • ThinkPHP使用心得分享-ThinkPHP + Ajax 实现2级联动下拉菜单

    首先是数据库的设计。分类表叫cate.我做的是分类数据的二级联动,数据需要的字段有:id,name(中文名),pid(父id). 父id的设置: 若数据没有上一级,则父id为0,若有上级,则父id为上一级的id。数据库有内容后,就可以开始写代码,进...2014-05-31
  • golang与php实现计算两个经纬度之间距离的方法

    这篇文章主要介绍了golang与php实现计算两个经纬度之间距离的方法,结合实例形式对比分析了Go语言与php进行经纬度计算的相关数学运算技巧,需要的朋友可以参考下...2016-07-29
  • PHP正则表达式过滤html标签属性(DEMO)

    这篇文章主要介绍了PHP正则表达式过滤html标签属性的相关内容,实用性非常,感兴趣的朋友参考下吧...2016-05-06
  • php构造方法中析构方法在继承中的表现

    这篇文章主要为大家详细介绍了php构造方法中析构方法在继承中的表现,感兴趣的小伙伴们可以参考一下...2016-04-15
  • PHP如何使用cURL实现Get和Post请求

    这篇文章主要介绍了PHP如何使用cURL实现Get和Post请求,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-07-11
  • PHP+jQuery+Ajax实现多图片上传效果

    今天我给大家分享的是在不刷新页面的前提下,使用PHP+jQuery+Ajax实现多图片上传的效果。用户只需要点击选择要上传的图片,然后图片自动上传到服务器上并展示在页面上。...2015-03-15
  • thinkPHP中多维数组的遍历方法

    这篇文章主要介绍了thinkPHP中多维数组的遍历方法,以简单实例形式分析了thinkPHP中foreach语句的使用技巧,需要的朋友可以参考下...2016-01-12
  • PHP简单实现生成txt文件到指定目录的方法

    这篇文章主要介绍了PHP简单实现生成txt文件到指定目录的方法,简单对比分析了PHP中fwrite及file_put_contents等函数的使用方法,需要的朋友可以参考下...2016-04-28
  • php图片添加文字水印实现代码

    这篇文章主要为大家详细介绍了php图片添加文字水印实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2016-03-17
  • php判断邮箱地址是否存在的方法

    这篇文章主要介绍了php判断邮箱地址是否存在的方法,php判断邮箱地址是否存在的方法有两种,感兴趣的朋友可以参考一下...2016-02-18
  • thinkphp自定义权限管理之名称判断方法

    下面小编就为大家带来一篇thinkphp自定义权限管理之名称判断方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2017-04-03
  • php switch 与 if else 区别

    在php中switch是选择,if else也有同理,但是它们肯定是有区别的,那么我们来看看它们两者的区别在哪里呢,下面先看switch case语句吧。 switch($id){ case 1: ...2016-11-25