php实现的Captcha验证码类实例
2015-01-24信息快讯网
这篇文章主要介绍了php实现的Captcha验证码类,实例展示了一个验证码类程序并附有用法演示实例,有着非常好的参考借鉴价值,需要的朋友可以参考下
本文实例讲述了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通过Chianz.com获取IP地址与地区的方法
WampServer下安装多个版本的PHP、mysql、apache图文教程
PHP PDO fetch 模式各种参数的输出结果一览
windows中为php安装mongodb与memcache
PHP中$this和$that指针使用实例
php下Memcached入门实例解析
php模仿asp Application对象在线人数统计实现方法
php从memcache读取数据再批量写入mysql的方法
PHP使用memcache缓存技术提高响应速度的方法
PHP连接MSSQL时nvarchar字段长度被截断为255的解决方法
PHP提示Warning:phpinfo() has been disabled函数禁用的解决方法
php常用hash加密函数
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 2611816 bytes)
CI框架验证码CAPTCHA辅助函数用法实例
php实现的click captcha点击验证码类实例
php生成QRcode实例
php中unserialize返回false的解决方法
php中strstr、strrchr、substr、stristr四个函数的区别总结
PHP提示Cannot modify header information - headers already sent by解决方法
PHP正则替换函数preg_replace和preg_replace_callback使用总结
CentOS 6.3下安装PHP xcache扩展模块笔记
CodeIgniter错误mysql_connect(): No such file or directory解决方法
Chrome Web App开发小结
PHP提示Deprecated: mysql_connect(): The mysql extension is deprecated的解决方法
PHP中echo和print的区别
PHP中使用localhost连接Mysql不成功的解决方法
PHP错误Allowed memory size of 67108864 bytes exhausted的3种解决办法
ThinkPHP行为扩展Behavior应用实例详解
php.ini save_handler 修改不生效的解决办法
kohana框架上传文件验证规则写法示例
PHP开发框架kohana中处理ajax请求的例子
PHP关于htmlspecialchars、strip_tags、addslashes的解释
php+highchats生成动态统计图
codeigniter框架The URI you submitted has disallowed characters错误解决方法
CodeIgniter框架提示Disallowed Key Characters的解决办法