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 相关文章推荐
优化NFR之一 --MSSQL Hello Buffer Overflow
Oct 09 PHP
建立动态的WML站点(二)
Oct 09 PHP
Uchome1.2 1.5 代码学习 common.php
Apr 24 PHP
深入php self与$this的详解
Jun 08 PHP
PHP与MYSQL中UTF8 中文排序示例代码
Oct 23 PHP
自己写的兼容低于PHP 5.5版本的array_column()函数
Oct 24 PHP
PHP判断浏览器、判断语言代码分享
Mar 05 PHP
php截取指定2个字符之间字符串的方法
Apr 15 PHP
PHP获取数组的键与值方法小结
Jun 13 PHP
php类自动装载、链式操作、魔术方法实现代码
Jul 23 PHP
PHP实现动态添加XML中数据的方法
Mar 30 PHP
tp5 实现列表数据根据状态排序
Oct 18 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将数据库导出成excel的方法
2010/05/07 PHP
php中3种方法统计字符串中每种字符的个数并排序
2012/08/27 PHP
php分页示例分享
2014/04/30 PHP
php中关于长度计算容易混淆的问题分析
2016/05/27 PHP
PHP5.5安装PHPRedis扩展及连接测试方法
2017/01/22 PHP
深入理解javascript严格模式(Strict Mode)
2014/11/28 Javascript
JS匿名函数类生成方式实例分析
2016/11/26 Javascript
JavaScript触发onScroll事件的函数节流详解
2016/12/14 Javascript
JS实现图片预览的两种方式
2017/06/27 Javascript
js如何编写简单的ajax方法库
2017/08/02 Javascript
webpack手动配置React开发环境的步骤
2018/07/02 Javascript
es6基础学习之解构赋值
2018/12/10 Javascript
layui 上传文件_批量导入数据UI的方法
2019/09/23 Javascript
tracking.js实现前端人脸识别功能
2020/04/16 Javascript
vue实现打地鼠小游戏
2020/08/21 Javascript
JS实现简易图片自动轮播
2020/10/16 Javascript
举例详解Python中的split()函数的使用方法
2015/04/07 Python
利用python 更新ssh 远程代码 操作远程服务器的实现代码
2018/02/08 Python
python二分法查找算法实现方法【递归与非递归】
2019/12/06 Python
pycharm实现在子类中添加一个父类没有的属性
2020/03/12 Python
使用PyQt的QLabel组件实现选定目标框功能的方法示例
2020/05/19 Python
Python实现哲学家就餐问题实例代码
2020/11/09 Python
python 爬虫请求模块requests详解
2020/12/04 Python
CSS3实现文本垂直排列的方法
2018/07/10 HTML / CSS
Html5游戏开发之乒乓Ping Pong游戏示例(二)
2013/01/21 HTML / CSS
美国鲍勃商店:Bob’s Stores
2018/07/22 全球购物
苏格兰领先的多渠道鞋店:Begg Shoes
2019/10/22 全球购物
应届生自荐信范文
2014/02/21 职场文书
法人代表授权委托书范文
2014/09/10 职场文书
逃课打麻将检讨书
2014/10/05 职场文书
银行求职自荐信范文
2015/03/04 职场文书
高考满分作文赏析(2篇)
2019/08/12 职场文书
Python数据可视化之绘制柱状图和条形图
2021/05/25 Python
mysql备份策略的实现(全量备份+增量备份)
2021/07/07 MySQL
MySQL约束超详解
2021/09/04 MySQL
三星 3nm 芯片将于第二季度开始量产
2022/04/29 数码科技