验证码类

论坛中的验证码的作用
因为你的WEB站有时会碰到客户机恶意攻击,其中一种很常见的攻击手段就是 身份欺骗_它通过

在客户端脚本写入一些代码,然后利用其,客户机在网站,论坛反复登陆,或者攻击者创建一个HTML窗体,其窗体如果包含了你注册窗体或发帖窗体等相同的字段,然后利用"http-post"传输数据到服务器,服务器会

执行相应的创建帐户,提交垃圾数据等操作,如果服务器本身不能有效验证并拒绝此非法操作,它会很严重耗费

其系统资源,降低网站性能甚至使程序崩溃.

而现在流行的判断访问WEB程序是合法用户还是恶意操作的方式,就是采用 一种叫 "字符校验"的技术.

WEB网站像现在的动网论坛,他采用达到方法是为客户提供一个包含随即字符串的图片,用户必须读取

这些字符串,然后随 登陆窗体或者发帖窗体等用户创建的窗体一起提交.

因为人的话,可以很容易读出图片中的数字,但如果是一段客户端攻击代码,通过一般手段是很难识别验证码的

这样可以确保当前访问是来自一个人而非机器.
验证码:就是将一串随机产生的数字或符号,生成一幅图片, 图片里加上一些干扰象素(防止OCR),由用户肉眼识别其中的验证码信息,输入表单提交网站验证,验证成功后才能使用某项功能。
作用:
验证码一般是防止有人利用机器人自动批量注册、对特定的注册用户用特定程序暴力破解方式进行不断的登陆、灌水。因为验证码是一个混合了数字或符号的图片,人眼看起来都费劲,机器识别起来就更困难。像百度贴吧未登录发贴要输入验证码大概是防止大规模匿名回帖的发生。


一般注册用户ID的地方以及各大论坛都要要输入验证码

常见的验证码
1>四位数字,随机的一数字字符串,最原始的验证码,验证作用几乎为零。
2>CSDN网站用户登录用的是GIF格式,目前常用的随机数字图片验证码。图片上的字符比较中规中矩,验证作用比上一个好。没有基本图形图像学知识的人,不可破!可惜读取它的程序,在CSDN使用它的第一天,好像就在论坛里发布了,真是可怜!
3>QQ网站用户登录用的是PNG格式,图片用的随机数字+随机大写英文字母,整个构图有点张扬,每刷新一次,每个字符还会变位置呢!有时候出来的图片,人眼都识别不了,厉害啊…
4>MS的hotmail申请时候的是BMP格式, 随机数字+随机大写英文字母+随机干扰像素+随机位置。
5>Google的Gmail注册时候的是JPG格式,随机英文字母+随机颜色+随机位置+随机长度。
6>其他各大论坛的是XBM格式,内容随机。
验证码的作用:有效防止这种问题对某一个特定注册用户用特定程序暴力破解方式进行不断的登陆尝试,实际上是用验证码是现在很多网站通行的方式(比如招商银行的网上个人银行,腾讯的QQ社区),我们利用比较简易的方式实现了这个功能。虽然登陆麻烦一点,但是对社区还来说这个功能还是很有必要,也很重要。但我们还是提醒大家主要保护自己的密码,尽量使用混杂了数字、字母、符号在内的6位以上密码,不要使用诸如1234之类的简单密码或者与用户名相同、类似的密码。 不要因为只是来iclub问问问题,就随意设置密码,保护你自己的密码也是保护你自己,免得你的账号给人盗用给自己带来不必要的麻烦。 ~
(1).验证码一般是防止批量注册的,人眼看起来都费劲,何况是机器。二像百度贴吧未登录发贴要输入验证码大概是防止大规模匿名回帖的发生目前,不少网站为了防止用户利用机器人自动注册、登录、灌水,都采用了验证码技术。所谓验证码,就是将一串随机产生的数字或符号,生成一幅图片, 图片里加上一些干扰象素(防止OCR),由用户肉眼识别其中的验证码信息,输入表单提交网站验证,验证成功后才能使用某项功能。
(2).一般注册用户ID的地方以及各大论坛都要要输入验证码
(3).常见的验证码
1,四位数字,随机的一数字字符串,最原始的验证码,验证作用几乎为零。2,CSDN网站用户登录用的是GIF格式,目前常用的随机数字图片验证码。图片上的字符比较中规中矩,验证作用比上一个好。没有基本图形图像学知识的人,不可破!可惜读取它的程序,在CSDN使用它的第一天,好像就在论坛里发布了,真是可怜!
3,QQ网站用户登录用的是PNG格式,图片用的随机数字+随机大写英文字母,整个构图有点张扬,每刷新一次,每个字符还会变位置呢!有时候出来的图片,人眼都识别不了,厉害啊…4,MS的hotmail申请时候的是BMP格式, 随机数字+随机大写英文字母+随机干扰像素+随机位置。
5,Google的Gmail注册时候的是JPG格式,随机英文字母+随机颜色+随机位置+随机长度。6,其他各大论坛的是XBM格式,内容随机。
(4)意义:不少网站为了防止用户利用机器人自动注册、登录、灌水,都采用了验证码技术。所谓验证码,就是将一串随机产生的数字或符号,生成一幅图片,图片里加上一些干扰象素(防止OCR),由用户肉眼识别其中的验证码信息,输入表单提交网站验证,验证成功后才能使用某项功能。
验证码的作用:有效防止这种问题对某一个特定注册用户用特定程序暴力破解方式进行不断的登陆尝试,实际上是用验证码是现在很多网站通行的方式(比如招商银行的网上个人银行,腾讯的QQ社区),我们利用比较简易的方式实现了这个功能。虽然登陆麻烦一点,但是对社区还来说这个功能还是很有必要,也很重要。但我们还是提醒大家主要保护自己的密码,尽量使用混杂了数字、字母、符号在内的6位以上密码,不要使用诸如1234之类的简单密码或者与用户名相同、类似的密码。 不要因为只是来iclub问问问题,就随意设置密码,保护你自己的密码也是保护你自己,免得你的账号给人盗用给自己带来不必要的麻烦。

一个比较全的验证码类(数字、字母、汉字以及数字字母混合)

 

代码
   
   
<? php
/* *
* The base class of checkcode image with GD lib
* @version 2010-03-15
* @author zhangyu
*/
class Vcode
{
private $_config ;
private $_im ;
private $_str ;
private $_font = " FONT_DIR.'/simhei.ttf' " ;

public function __construct()
{
$this -> _config[ ' width ' ] = 140 ;
$this -> _config[ ' height ' ] = 50 ;
$this -> _config[ ' vcode ' ] = " vcode " ;
$this -> _config[ ' type ' ] = " default " ;
$this -> _config[ ' length ' ] = 4 ;
$this -> _config[ ' interfere ' ] = 10 ;
$this -> _config[ ' z ' ] = 210 ;
$this -> _str[ ' default ' ] = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 " ;
$this -> _str[ ' string ' ] = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz " ;
$this -> _str[ ' int ' ] = " 0123456789 " ;
$this -> _str[ ' varchinese ' ] = iconv ( ' utf-8 ' , ' GB2312 ' , " 二三四五好一家人与用途可程序爱上对方反弹过后让他时代感 " );
}

/* *
*配置
*@param array $_config('width'...)
*@return void
*/
public function init( $_config = array ())
{
if ( ! empty ( $_config ) && is_array ( $_config ))
{
foreach ( $_config as $key => $value )
{
$this -> _config[ $key ] = $value ;
}
}
}

/* *
*create image 输出验证码
*@return void
*/
public function create()
{
if ( ! function_exists ( " imagecreate " ))
{
return false ;
}
$this -> _create_do();
}

/* *
*create checkcode生成验证码
*return bool
*/
private function _create_do()
{
$this -> _im = imagecreate( $this -> _config[ ' width ' ] , $this -> _config[ ' height ' ]);

// 设置背景色
//设置背景边框

imagecolorallocate( $this -> _im , 255 , 255 , 255 );
$bordercolor = imagecolorallocate( $this -> _im , 37 , 37 , 37 );
imagerectangle(
$this -> _im , 0 , 0 , $this -> _config[ ' width ' ] - 1 , $this -> _config[ ' height ' ] - 1 , $bordercolor );

// 生成验证码
$this -> _create_str();
$vcode = $this -> _config[ ' vcode ' ];

// 输入验证码信息
$fontcolor = imagecolorallocate( $this -> _im , 46 , 46 , 46 );
for ( $i = 0 ; $i < $this -> _config[ ' length ' ]; $i ++ )
{
imagestring(
$this -> _im , 5 , $i * 10 + 6 , rand ( 2 , 5 ) , $vcode [ $i ] , $fontcolor );
}

$this -> _disturb();
header ( " Pragma:no-cache\r\n " );
header ( " Cache-Control:no-cache\r\n " );
header ( " Expires:0\r\n " );
header ( " content-type:image/jpeg/gif/png/bmp\r\n " );
imagejpeg(
$this -> _im);
imagedestroy(
$this -> _im);
return ture;
}
/* *
*set disturb
*@return void
*/
private function _disturb()
{
$interfere = $this -> _config[ ' interfere ' ];
$interfere = $interfere > 30 ? " 30 " : $interfere ;
if ( ! empty ( $interfere ) && $interfere > 1 )
{
for ( $i = 1 ; $i < $interfere ; $i ++ )
{
$linecolor = imagecolorallocate( $this -> _im , rand ( 0 , 255 ) , rand ( 0 , 255 ) , rand ( 110 , 255 ));
$x = rand ( 1 , $this -> _config[ ' width ' ]);
$y = rand ( 1 , $this -> _config[ ' height ' ]);
$x2 = rand ( $x - $this -> _config[ ' z ' ] , $x + $this -> _config[ ' z ' ]);
$y2 = rand ( $y - $this -> _config[ ' z ' ] , $y + $this -> _config[ ' z ' ]);
imageline(
$this -> _im , $x , $y , $x2 , $y2 , $linecolor );
}
}
}
/* *
*生成兼容性图片到浏览器
*创建中文验证码
*@return array
*/
private function _varchinese()
{

if ( function_exists ( " imagegif " ))
{
header ( " Content-type : image/gif " );

}
elseif ( function_exists ( " imagepng " ))
{
header ( " Content-type: image/png " );

}
elseif ( function_exists ( " imagejpeg " ))
{
header ( " Content-type: image/jpeg " );

}
elseif ( function_exists ( " imagewbmp " ))
{
header ( " Content-type: image/vnd.wap.wbmp " );

}
elseif ( function_exists ( " imagebmp " ))
{
header ( " Content-type: image/bmp " );

}
$_str = $this -> _str[ ' varchinese ' ] ;
$imgWidth = $this -> _config[ ' width ' ];
$imgHeight = $this -> _config[ ' height ' ];
$authimg = imagecreate( $imgWidth , $imgHeight );
$bgColor = ImageColorAllocate( $authimg , 255 , 255 , 255 );
$fontfile = $this -> _font;
$white = imagecolorallocate( $authimg , 11 , 12 , 12 );
imagearc(
$authimg , 55 , 8 , 20 , 20 , 15 , 11 , $white );
imagearc(
$authimg , 55 , 7 , 50 , 30 , 75 , 11 , $white );
imageline(
$authimg , 20 , 20 , 55 , 30 , $white );
imageline(
$authimg , 20 , 18 , 22 , 50 , $white );
imageline(
$authimg , 25 , 50 , 80 , 50 , $white );
$noise_num = 800 ;
$line_num = 20 ;
imagecolorallocate(
$authimg , 0xff , 0xff , 0xff );
$rectangle_color = imagecolorallocate( $authimg , 0xAA , 0xAA , 0xAA );
$noise_color = imagecolorallocate( $authimg , 0x00 , 0x00 , 0x00 );
$font_color = imagecolorallocate( $authimg , 0x00 , 0x00 , 0x00 );
$line_color = imagecolorallocate( $authimg , 0x00 , 0x00 , 0x00 );
for ( $i = 0 ; $i < $noise_num ; $i ++ )
{
imagesetpixel(
$authimg , mt_rand ( 0 , $imgWidth ) , mt_rand ( 0 , $imgHeight ) , $noise_color );
}
for ( $i = 0 ; $i < $line_num ; $i ++ )
{
imageline(
$authimg , mt_rand ( 0 , $imgWidth ) , mt_rand ( 0 , $imgHeight ) , mt_rand ( 0 , $imgWidth ) , mt_rand ( 0 , $imgHeight ) , $line_color );
}
$randnum = rand ( 0 , strlen ( $_str ) - 4 );
if ( $randnum % 2 )
{
$randnum += 1 ;
}
$_str = substr ( $_str , $randnum , 8 );
$_str = iconv ( " GB2312 " , " UTF-8 " , $_str );
ImageTTFText(
$authimg , 20 , 5 , 9 , 30 , $font_color , $fontfile , $_str );
ImagePNG(
$authimg );
ImageDestroy(
$authimg );
return $vcode ;
}


/* *
*通过条件输出验证码信息
*@return bool
*/
private function _create_str()
{
if ( $this -> _config[ ' type ' ] == " int " )
{
for ( $i = 1 ; $i <= $this -> _config[ ' length ' ]; $i ++ )
{
$vcode .= rand ( 0 , 9 );
}
$this -> _config[ ' vcode ' ] = $vcode ;
return true ;
}
else if ( $this -> _config[ ' type ' ] == " varchinese " )
{
$code = new vcode();
$this -> _config[ ' vcode ' ] = $code -> _varchinese();
return true ;
}
$len = strlen ( $this -> _str[ $this -> _config[ ' type ' ]]);
if ( ! $len )
{
$this -> _config[ ' type ' ] = " default " ;
$this -> _create_str();
}
for ( $i = 1 ; $i <= $this -> _config[ ' length ' ]; $i ++ )
{
$offset = rand ( 0 , $len - 1 );
$vcode .= substr ( $this -> _str[ $this -> _config[ ' type ' ]] , $offset , 1 );
}
$this -> _config[ ' vcode ' ] = $vcode ;
return true ;
}
}
?>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值