PHP封装的验证码工具类定义与用法示例

yipeiwu_com6年前PHP代码库

本文实例讲述了PHP封装的验证码工具类定义与用法。分享给大家供大家参考,具体如下:

下面分享的是我自己封装的验证码工具类,在平时的项目中会比较经常用到的工具类,目前封装的这个工具类简易版的,如果有需要的伙伴可以拿去用,当然我建议用之前在配置文件里增加一些选项信息

//验证码宽度
private $width;
//验证码高度
private $height;
//验证的个数
private $length;
//干扰点个数
private $dots;
//干扰点的类型
private $type;
//干扰线个数
private $lines;
//文字
private $font;

方便在项目中对验证码的要求进行更改,以方便项目逻辑的需求,而且验证码所选用的字体需要和验证码工具类放在同一目录下,否则就要在配置文件中修改字体的路径才能实现验证码的显示

<?php
//创建验证码工具类
class captcha {
 //验证码的各种参数
 //验证码宽度
 private $width;
 //验证码高度
 private $height;
 //验证的个数
 private $length;
 //干扰点个数
 private $dots;
 //干扰点的类型
 private $type;
 //干扰线个数
 private $lines;
 //文字
 private $font;
 //验证码属性的构造方法
 public function __construct($arr = array ()) {
  //将属性赋值
  $this->width = isset ($arr['width']) ? trim($arr['width']) : '270';
  $this->height = isset ($arr['height']) ? trim($arr['height']) : '30';
  $this->length = isset ($arr['length']) ? trim($arr['length']) : '4';
  $this->dots = isset ($arr['dots']) ? trim($arr['dots']) : '81';
  $this->type = isset ($arr['type']) ? trim($arr['type']) : '*';
  $this->lines = isset ($arr['lines']) ? trim($arr['lines']) : '5';
  $this->font = isset ($arr['font']) ? trim($arr['font']) : './cambriab.ttf';
 }
 //创建验证码的方法
 public function captcha() {
  //创建画布
  $img = imagecreatetruecolor($this->width, $this->height);
  //填充颜色
  //颜色资源
  $color = imagecolorallocate($img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
  //填充背景
  imagefill($img, 0, 0, $color);
  //添加干扰点
  for ($i = 0; $i < $this->dots; $i++) {
   //颜色资源
   $dots_color = imagecolorallocate($img, mt_rand(150, 200), mt_rand(150, 200), mt_rand(150, 200));
   //插入干扰点
   imagestring($img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), $this->type, $dots_color);
  }
  //添加干扰线
  for ($i = 0; $i < $this->lines; $i++) {
   //颜色资源
   $line_color = imagecolorallocate($img, mt_rand(150, 200), mt_rand(150, 200), mt_rand(150, 200));
   //插入干扰线
   imageline($img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $line_color);
  }
  //首先获取验证码,然后插入验证文字
  //文字高度
  $size = mt_rand(18, 20);
  //获取验证码
  $str = $this->captchastring();
  for ($i = 0; $i < strlen($str); $i++) {
   //颜色资源
   $str_color = imagecolorallocate($img, mt_rand(50, 150), mt_rand(50, 150), mt_rand(50, 150));
   //插入验证码
   imagettftext($img, $size, mt_rand(-45, 45), $this->width / ($this->length + 2) * ($i +1), (($this->height - $size) / 2) + $size, $str_color, $this->font, $str[$i]);
  }
  //将得到的验证码存入SESSION中,便于以后的验证码判断
  @ session_start();
  $_SESSION['captcha'] = $str;
  //输出图片
  header("content-type:image/png");
  imagepng($img);
  //清除资源
  imagedestroy($img);
 }
 //获取随机的验证内容:A-Z,a-z,1-9
 private function captchaString() {
  //获取四个随机的字符串
  $str = "";
  for ($i = 0; $i < $this->length; $i++) {
   switch (mt_rand(1, 3)) {
    case 1 :
     $str .= chr(mt_rand(49, 57));
     break;
    case 2 :
     $str .= chr(mt_rand(97, 122));
     break;
    case 3 :
     $str .= chr(mt_rand(65, 90));
     break;
   }
  }
  return $str;
 }
 //判断验证码
 public static function checkCaptcha($captcha) {
  @ session_start();
  return strtoupper($captcha) === strtoupper($_SESSION['captcha']);
 }
}
//使用方法:
$img = new captcha();//这里采用默认参数
$img->captcha();
?>

运行结果:

注:代码中用到的字体为cambriab.ttf可完整实例代码点击此处本站下载

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP图形与图片操作技巧汇总》、《PHP数学运算技巧总结》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》及《php字符串(string)用法总结

希望本文所述对大家PHP程序设计有所帮助。

相关文章

php中如何使对象可以像数组一样进行foreach循环

刚接触到题的时候,我也没有考虑到Iterator模式,试了几个一般想法,失败以后。。。。就直接去翻看了foreach的源码实现,期望发现foreach处理对象的时候是否有什么特殊性,可以...

关于file_get_contents返回为空或函数不可用的解决方案

如果你使用file_get_contents获取远程文件内容返回为空或提示该函数不可用,也许本文能帮到你! 使用file_get_contents和fopen必须空间开启allow_ur...

PHP使用Pear发送邮件(Windows环境)

PHP使用Pear发送邮件(Windows环境)

本文实例介绍了PHP使用Pear发送邮件的方法,以及如何在Windows下安装PHP Pear的步骤,分享给大家供大家参考,具体内容如下 一、PEAR安装 1、PEAR简介 PEAR是P...

基于php中使用excel的简单介绍

基于php中使用excel的简单介绍

在工作中需要处理多语言的翻译问题,翻译都是写在excel表格里面。为了处理方便我就保留中文和英文两列。 这样需要将这些数据从excel中取出来,然乎保存在excel的数组中,通过使用循环...

PHP开发工具ZendStudio下Xdebug工具使用说明详解

从学习PHP基础知识到使用ZendSudio工具开发有半年多了,这过程中,学习断断续续,遇到的问题也是N多,学习PHP还真是不让人省心。最不省心的还是算调试过程。 学习编程,一定要懂得调...