php实现的Captcha验证码类实例


Posted in PHP onSeptember 22, 2014

本文实例讲述了php实现的Captcha验证码类,在php程序设计中有着极其广泛的应用。分享给大家供大家参考。具体方法如下:

验证码类文件如下:

<?php
/** Captcha 验证码类
* Date: 2011-02-19
* Author: fdipzone
*/

class Captcha{ //class start

 private $sname = '';

 public function __construct($sname=''){ // $sname captcha session name
 $this->sname = $sname==''? 'm_captcha' : $sname;
 }

 /** 生成验证码图片
 * @param int $length 验证码长度
 * @param Array $param ??
 * @return IMG
 */
 public function create($length=4,$param=array()){
 Header("Content-type: image/PNG");
 $authnum = $this->random($length); //生成验证码字符.
 
 $width = isset($param['width'])? $param['width'] : 13; //文字宽度
 $height = isset($param['height'])? $param['height'] : 18; //文字高度
 $pnum = isset($param['pnum'])? $param['pnum'] : 100; //干扰象素个数
 $lnum = isset($param['lnum'])? $param['lnum'] : 2; //干扰线条数

 $this->captcha_session($this->sname,$authnum);  //将随机数写入session

 $pw = $width*$length+10;
 $ph = $height+6;
  
 $im = imagecreate($pw,$ph);   //imagecreate() 新建图像,大小为 x_size 和 y_size 的空白图像。
 $black = ImageColorAllocate($im, 238,238,238); //设置背景颜色
 
 $values = array(
  mt_rand(0,$pw), mt_rand(0,$ph),
  mt_rand(0,$pw), mt_rand(0,$ph),
  mt_rand(0,$pw), mt_rand(0,$ph),
  mt_rand(0,$pw), mt_rand(0,$ph),
  mt_rand(0,$pw), mt_rand(0,$ph),
  mt_rand(0,$pw), mt_rand(0,$ph)
 );
 imagefilledpolygon($im, $values, 6, ImageColorAllocate($im, mt_rand(170,255),mt_rand(200,255),mt_rand(210,255))); //设置干扰多边形底图
 
 /* 文字 */
 for ($i = 0; $i < strlen($authnum); $i++){
  $font = ImageColorAllocate($im, mt_rand(0,50),mt_rand(0,150),mt_rand(0,200));//设置文字颜色
  $x = $i/$length * $pw + rand(1, 6); //设置随机X坐标
  $y = rand(1, $ph/3);   //设置随机Y坐标
  imagestring($im, mt_rand(4,6), $x, $y, substr($authnum,$i,1), $font); 
 }

 /* 加入干扰象素 */
 for($i=0; $i<$pnum; $i++){
  $dist = ImageColorAllocate($im, mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); //设置杂点颜色
  imagesetpixel($im, mt_rand(0,$pw) , mt_rand(0,$ph) , $dist); 
 } 

 /* 加入干扰线 */
 for($i=0; $i<$lnum; $i++){
  $dist = ImageColorAllocate($im, mt_rand(50,255),mt_rand(150,255),mt_rand(200,255)); //设置线颜色
  imageline($im,mt_rand(0,$pw),mt_rand(0,$ph),mt_rand(0,$pw),mt_rand(0,$ph),$dist);
 }

 ImagePNG($im); //以 PNG 格式将图像输出到浏览器或文件
 ImageDestroy($im); //销毁一图像
 }

 /** 检查验证码
 * @param String $captcha 验证码
 * @param int $flag 验证成功后 0:不清除session 1:清除session
 * @return boolean
 */ 
 public function check($captcha,$flag=1){
 if(empty($captcha)){
  return false;
 }else{
  if(strtoupper($captcha)==$this->captcha_session($this->sname)){ //检测验证码
  if($flag==1){
   $this->captcha_session($this->sname,'');
  }
  return true;
  }else{
  return false;
  }
 }
 }

 /* 产生随机数函数
 * @param int $length 需要随机生成的字符串?
 * @return String
 */
 private function random($length){
 $hash = '';
 $chars = 'ABCDEFGHIJKLMNPQRSTUVWXYZ23456789';
 $max = strlen($chars) - 1;
 for($i = 0; $i < $length; $i++) {
  $hash .= $chars[mt_rand(0, $max)];
 }
 return $hash;
 }

 /** 验证码session处理方法
 * @param String $name captcha session name
 * @param String $value
 * @return String
 */
 private function captcha_session($name,$value=null){
 if(isset($value)){
  if($value!==''){
  $_SESSION[$name] = $value;
  }else{
  unset($_SESSION[$name]);
  }
 }else{
  return isset($_SESSION[$name])? $_SESSION[$name] : '';
 }
 }
} // class end
?>

demo示例程序如下:

<?php 
  session_start(); 
  require_once('Captcha.class.php'); 
 
  $obj = new Captcha($sname);   # 创建Captcha类对象 
                  # $sname为保存captcha的session name,可留空,留空?t为'm_captcha' 
 
  $obj->create($length,$param);  #创建Captcha并输出图片 
                  # $length为Captcha长度,可留空,默认为4 
                  /* $param = array( 
                      'width' => 13    captcha 字符宽度 
                      'height' => 18    captcha 字符高度 
                      'pnum' => 100    干扰点个数
                      'lnum' => 2     干扰线条数 
                      ) 
                      可留空 
                  */ 
  $obj->check($captcha,$flag); # 检查用户输入的验证码是否正确,true or false 
                  # $captcha为用户输入的验证码,必填 
                  # $flag 可留空,默认为1  
                  #    1:当验证成功后自动清除captcha session 
                  #    0:挡验证成功后不清除captcha session,用于ajax检查 
?>

相信本文所述对大家php程序设计的学习有一定的借鉴价值。

PHP 相关文章推荐
PHP截取中文字符串的问题
Jul 12 PHP
PHP 高手之路(三)
Oct 09 PHP
图片存储与浏览一例(Linux+Apache+PHP+MySQL)
Oct 09 PHP
php读取msn上的用户信息类
Dec 05 PHP
smarty中先strip_tags过滤html标签后truncate截取文章运用
Oct 25 PHP
smarty模板引擎从配置文件中获取数据的方法
Jan 22 PHP
WordPress中&quot;无法将上传的文件移动至&quot;错误的解决方法
Jul 01 PHP
PHP5.5.15+Apache2.4.10+MySQL5.6.20配置方法分享
May 06 PHP
浅析Laravel5中队列的配置及使用
Aug 04 PHP
php使用glob函数遍历文件和目录详解
Sep 23 PHP
php each 返回数组中当前的键值对并将数组指针向前移动一步实例
Nov 22 PHP
Thinkphp5框架ajax接口实现方法分析
Aug 28 PHP
php中unserialize返回false的解决方法
Sep 22 #PHP
php实现根据字符串生成对应数组的方法
Sep 22 #PHP
PHP中auto_prepend_file与auto_append_file用法实例分析
Sep 22 #PHP
php中Y2K38的漏洞解决方法实例分析
Sep 22 #PHP
php中strstr、strrchr、substr、stristr四个函数的区别总结
Sep 22 #PHP
PHP中常用的输出函数总结
Sep 22 #PHP
C#静态方法与非静态方法实例分析
Sep 22 #PHP
You might like
PHP中时间加减函数strtotime用法分析
2017/04/26 PHP
让浏览器非阻塞加载javascript的几种方法小结
2011/04/25 Javascript
JavaScript使表单中的内容显示在屏幕上的方法
2015/06/29 Javascript
JavaScript对象数组排序实例方法浅析
2016/06/15 Javascript
手机端图片缩放旋转全屏查看PhotoSwipe.js插件实现
2016/08/25 Javascript
简单实现node.js图片上传
2016/12/18 Javascript
使用Javascript判断浏览器终端设备(PC、IOS(iphone)、Android)
2017/01/04 Javascript
深入理解JavaScript中的for循环
2017/02/07 Javascript
js实现省份下拉菜单效果
2017/02/15 Javascript
Node接收电子邮件的实例代码
2017/07/21 Javascript
Vue中使用Sortable的示例代码
2018/04/07 Javascript
jQuery实现每隔一段时间自动更换样式的方法分析
2018/05/03 jQuery
使用vue-cli脚手架工具搭建vue-webpack项目
2019/01/14 Javascript
ES6 Generator基本使用方法示例
2020/06/06 Javascript
微信小程序实现转盘抽奖
2020/09/21 Javascript
[03:46]DOTA2英雄基础教程 维萨吉
2013/12/11 DOTA
[14:20]刀塔大凶女神互压各路奇葩屌丝
2014/05/16 DOTA
[47:38]Optic vs VGJ.S 2018国际邀请赛小组赛BO2 第二场 8.17
2018/08/20 DOTA
[01:20]DOTA2更新全新英雄 天涯墨客现已加入游戏
2018/08/25 DOTA
浅谈function(函数)中的动态参数
2017/04/30 Python
python使用opencv读取图片的实例
2017/08/17 Python
Python实现字符串匹配算法代码示例
2017/12/05 Python
Python实现的NN神经网络算法完整示例
2018/06/19 Python
Django 限制用户访问频率的中间件的实现
2018/08/23 Python
利用Python实现原创工具的Logo与Help
2018/12/03 Python
浅谈python标准库--functools.partial
2019/03/13 Python
tensorflow mnist 数据加载实现并画图效果
2020/02/05 Python
如何基于Python实现数字类型转换
2020/02/07 Python
用60行代码实现Python自动抢微信红包
2021/02/04 Python
利用Python批量识别电子账单数据的方法
2021/02/08 Python
添柏岚英国官方网站:Timberland英国
2019/11/28 全球购物
房地产开发计划书
2014/01/10 职场文书
团结演讲稿范文
2014/05/23 职场文书
蛋糕店创业计划书范文
2014/09/21 职场文书
领导干部贪图享乐整改措施
2014/09/21 职场文书
教师聘用意向书
2015/05/11 职场文书