个人写的PHP验证码生成类分享


Posted in PHP onAugust 21, 2014

此验证码类直接拿去就可以用,也可以参考!

其中类成员codestr是生成的验证码字符串:

<?php
/**
 * 验证码
 */
class Code{
 
  // 1. 定义各个成员 有宽、高、画布、字数、类型、画类型
   
  private $width; //宽度
  private $height; //高度
  private $num; //验证码字数
  private $imgType; //生成图片类型
  private $Type; //字串类型 1,2,3 三个选项 1 纯数字 2 纯小写字母 3 大小写数字混合
  private $hb; //画布
  public $codestr; // 验证码字串
 
  public function __construct($height=20,$num=4,$imgType="jpeg",$Type=1){
    $this->width = $num*20;
    $this->height = $height;
    $this->num = $num;
    $this->imgType = $imgType;  
    $this->Type = $Type; 
    $this->codestr = $this->codestr();
    $this->zuhe();
  }
 
  // 2. 定义随机获取字符串函数
  private function codestr(){
    switch($this->Type){
     
      case 1:   // 类型为1 获取1-9随机数
        $str = implode("",array_rand(range(0,9),$this->num));
        break;
      case 2:   // 类型为2 获取a-z随机小写字母
        $str = implode("",array_rand(array_flip(range(a,z)),$this->num));
        break;
      case 3:   // 类型为3 获取数字,小写字母,大写字母 混合
        for($i=0;$i<$this->num;$i++){
          $m = rand(0,2);
          switch($m){
            case 0:
              $o = rand(48,57);
              break;
            case 1:
              $o = rand(65,90);
              break;
            case 2:
              $o = rand(97,122);
              break; 
          }
          $str .= sprintf("%c",$o);
        }
        break;     
    }
 
     
    return $str;  
  }
 
 
  // 3. 初始化画布图像资源
  private function Hb(){
    $this->hb = imagecreatetruecolor($this->width,$this->height); 
  }
 
  // 4. 生成背景颜色
  private function Bg(){
    return imagecolorallocate($this->hb,rand(130,250),rand(130,250),rand(130,250)); 
  }
 
  // 5. 生成字体颜色
  private function Font(){
    return imagecolorallocate($this->hb,rand(0,100),rand(0,100),rand(0,100));  
  }
 
  // 6. 填充背景颜色
  private function BgColor(){
    imagefilledrectangle($this->hb,0,0,$this->width,$this->height,$this->Bg()); 
  }
 
  // 7. 干扰点
  private function ganrao(){
    $sum=floor(($this->width)*($this->height)/3);
    for($i=0;$i<$sum;$i++){
      imagesetpixel($this->hb,rand(0,$this->width),rand(0,$this->height),$this->Bg());  
    }
  }
 
  // 8. 随机直线 弧线
  private function huxian(){
    for($i=0;$i<$this->num;$i++){
      imageArc($this->hb,rand(0,$this->width),rand(0,$this->height),rand(0,$this->width),rand(0,$this->height),rand(0,360),rand(0,360),$this->Bg());    
    }  
  }
 
  // 9. 写字
  private function xiezi(){
    for($i=0;$i<$this->num;$i++){
      $x=ceil($this->width/$this->num)*$i; 
      $y=rand(1,$this->height-15);
      imagechar($this->hb,5,$x+4,$y,$this->codestr[$i],$this->Font());
    }  
  }
 
  // 10. 输出
  private function OutImg(){
    $shuchu="image".$this->imgType; 
    $header="Content-type:image/".$this->imgType;
    if(function_exists($shuchu)){
      header($header);
      $shuchu($this->hb); 
    }else{
      exit("GD库没有此类图像"); 
    }
  }
 
  // 11. 拼装
  private function zuhe(){
    $this->Hb();
    $this->BgColor();
    $this->ganrao();
    $this->huxian();
    $this->xiezi();
    $this->OutImg(); 
  }  
 
  public function getCodeStr(){
    return $this->codestr;    
  }
}
?>
PHP 相关文章推荐
一个域名查询的程序
Oct 09 PHP
MySQL数据库转移,access,sql server 转 MySQL 的图文教程
Sep 02 PHP
php中几种常见安全设置详解
Apr 06 PHP
mysqli_set_charset和SET NAMES使用抉择及优劣分析
Jan 13 PHP
ECMall支持SSL连接邮件服务器的配置方法详解
May 19 PHP
简单分析ucenter 会员同步登录通信原理
Aug 25 PHP
Linux环境下php实现给网站截图的方法
May 03 PHP
php使用escapeshellarg时中文被过滤的解决方法
Jul 10 PHP
zen cart实现订单中增加paypal中预留电话的方法
Jul 12 PHP
php魔法函数与魔法常量使用介绍
Jul 23 PHP
php微信开发之关键词回复功能
Jun 13 PHP
PHP实现创建一个RPC服务操作示例
Feb 23 PHP
PHP中使用sleep造成mysql读取失败的案例和解决方法
Aug 21 #PHP
从零开始学YII2框架(六)高级应用程序模板
Aug 20 #PHP
ThinkPHP登录功能的实现方法
Aug 20 #PHP
从零开始学YII2框架(五)快速生成代码工具 Gii 的使用
Aug 20 #PHP
PHP面向对象程序设计之类常量用法实例
Aug 20 #PHP
从零开始学YII2框架(四)扩展插件yii2-kartikgii
Aug 20 #PHP
PHP面向对象程序设计之接口用法
Aug 20 #PHP
You might like
浅析HTTP消息头网页缓存控制以及header常用指令介绍
2013/06/28 PHP
php数组合并的二种方法
2014/03/21 PHP
PHP判断表单复选框选中状态完整例子
2014/06/24 PHP
一款简单实用的php操作mysql数据库类
2014/12/08 PHP
PHP自毁程序(慎用)
2015/07/09 PHP
解决laravel groupBy 对查询结果进行分组出现的问题
2019/10/09 PHP
jquery $.ajax入门应用二
2008/11/19 Javascript
五个jQuery图片画廊插件 推荐
2011/05/12 Javascript
javascript中拼接HTML字符串的最快、最好的方法
2014/06/07 Javascript
js动态修改表格行colspan列跨度的方法
2015/03/30 Javascript
js限制input标签中只能输入中文
2015/06/26 Javascript
jquery zTree异步加载简单实例讲解
2016/02/25 Javascript
详细解读Jquery各Ajax函数($.get(),$.post(),$.ajax(),$.getJSON())
2016/08/15 Javascript
jquery pagination插件动态分页实例(Bootstrap分页)
2016/12/23 Javascript
JavaScript基本语法_动力节点Java学院整理
2017/06/26 Javascript
vue2.0开发入门笔记之.vue文件的生成和使用
2017/09/19 Javascript
微信小程序实现聊天对话(文本、图片)功能
2018/07/06 Javascript
express如何解决ajax跨域访问session失效问题详解
2019/06/20 Javascript
vue的hash值原理也是table切换实例代码
2020/12/14 Vue.js
Python中Class类用法实例分析
2015/11/12 Python
pandas 实现字典转换成DataFrame的方法
2018/07/04 Python
Python使用pandas对数据进行差分运算的方法
2018/12/22 Python
python使用 zip 同时迭代多个序列示例
2019/07/06 Python
python使用paramiko模块通过ssh2协议对交换机进行配置的方法
2019/07/25 Python
Python从MySQL数据库中面抽取试题,生成试卷
2021/01/14 Python
html5视频常用API接口的实战示例
2020/03/20 HTML / CSS
汽车检测与维修个人求职信
2013/09/24 职场文书
幼儿园教师培训制度
2014/01/16 职场文书
家长给小学生的评语
2014/01/30 职场文书
给国外客户的邀请函
2014/01/30 职场文书
毕业评语大全
2014/05/04 职场文书
践行三严三实心得体会
2014/10/13 职场文书
股东大会通知
2015/04/24 职场文书
公司2015年终工作总结
2015/05/26 职场文书
教研活动主持词
2015/07/03 职场文书
2016新年问候语大全
2015/11/11 职场文书