本文实例讲述了php实现的Captcha验证码类,在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

$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

$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示例程序如下:

session_start();

require_once('Captcha.class.php');

$obj = new Captcha($sname); # 创建Captcha类对象

# $sname为保存captcha的session name,可留空,留空則为'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程序设计的学习有一定的借鉴价值。

3dcaptcha php,php实现的Captcha验证码类实例相关推荐

  1. php clicaptcha,php实现的Captcha验证码类实例

    这篇文章主要介绍了php实现的Captcha验证码类,实例展示了一个验证码类程序并附有用法演示实例,有着非常好的参考借鉴价值,需要的朋友可以参考下 本文实例讲述了php实现的Captcha验证码类,在 ...

  2. 3dcaptcha php,php Captcha验证码类

    1,Captcha验证码类 复制代码 代码示例: /** Captcha 验证码类 *   Date:   2011-02-19 *   Author: fdipzone *   Edit: www. ...

  3. [验证码实现] Captcha 验证码类,一个很个性的验证码类 (转载)

    点击下载 Captcha.zip /// <summary> /// 类说明:条码生成类 /// 编 码 人:苏飞 /// 联系方式:361983679 /// 更新网站:[url=htt ...

  4. php缓存实例,一个PHP缓存类实例

    一个PHP缓存类实例 发布于 2014-08-05 21:44:28 | 104 次阅读 | 评论: 0 | 来源: 网友投递 PHP开源脚本语言PHP(外文名: Hypertext Preproce ...

  5. php最简单验证码代码,简单好用的PHP验证码类

    /** * @PHP验证码类 * 使用方法: * $image=new Captcha(); * $image->config('宽度','高度','字符个数','验证码session索引'); ...

  6. PHP使用GD库封装验证码类

    调试小技巧:当图片无法显示时,将header函数注释掉就可以看到报错信息了 字体文件放在当前文件目录的font文件夹中,windows的字体可以到C:\Windows\Fonts目录下复制过来,处理好 ...

  7. laravel图形验证码(借用了TP的图形验证码类)非常好用简单,非常适合前后端分离的项目

    class Captcha extends Controller {//protected $config = array('seKey' => 'jjh', // 验证码加密密钥'codeSe ...

  8. (二十三)admin-boot项目之captcha验证码整合

    (二十三)captcha验证码整合 项目地址:https://gitee.com/springzb/admin-boot 如果觉得不错,给个 star 简介: 这是一个基础的企业级基础后端脚手架项目, ...

  9. php验证码类(分享)

    //验证码类 class ValidateCode {private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';/ ...

最新文章

  1. [ZJOI2010]数字计数
  2. Java多线程_阻塞队列
  3. 2018年云栖社区值得订阅的11个精选技术期刊!
  4. python刷新_如何在python中刷新输入流?
  5. uitextfield长按显示英文select all,copy,paste?
  6. 5.1作业5 四则运算 测试与封装
  7. educoder实训平台python顺序结构答案_传智播客升级实训课程,打造高分项目实战网课助力高校在线实训...
  8. Python删除文件第一行
  9. WIN2008R2 激活
  10. php 中文字符串长度_js或php获取字符串长度中文1个字符,英文0.5个字符
  11. 用友U8修改货位现存量
  12. mybatis系列-03-入门程序
  13. Linux中etc目录etc是什么单词的缩写
  14. eovs实训报告总结心得_实训报告收获心得体会
  15. 红帽与Cloudera结成大数据联盟 释放企业级Hadoop潜能
  16. strcpy_s与strcpy
  17. 32位程序和64位程序这些区别你知道吗?
  18. UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 0: ordinal not in range(128)
  19. 【本周Python热点回顾】画一棵漂亮的樱花树,Python3*和**运算符,Python入门,这就是Python3.8么,i了
  20. Symfony2浅析

热门文章

  1. 这10种创意图表,能让可视化报告瞬间变得惊艳炫酷,5分钟学会
  2. 【小点点】上架了他们的官方Windows 8应用
  3. JAVASCRIPT干了不下四五种工作
  4. 即时通讯软件在企业里的应用及发展
  5. 暴雪BN2.0 呼之欲出即将到来
  6. VC++实现混合静态分裂视窗的方法
  7. Python 基础,不看会后悔哦!
  8. 你当初是如何走上编程之路的?
  9. 谷歌放弃C++语言,Python将要一统江湖了?
  10. 当了几年程序员,是时候考虑以后的发展了