手机版

PHP验证码类ValidateCode分析

时间:2021-09-06 来源:互联网 编辑:宝哥软件园 浏览:

PHP解析验证码类

1.开始

在网上看到用PHP写的ValidateCode生成验证码类的感觉很好,所以想分析学习一下。

2.类图

3.验证码类的部分代码

3.1定义变量

//random factor private $ charset=' abcdefghjkmrstuvwxyzabbcdefgjkmrstutuvwxyz 23456789 ';私有$代码;private $ codeLen=4;private $ width=130private $ heigh=50private $ img//image private $ font;//font private $ font size=20;$charset是一个随机因子。这里去掉了一些无法区分的字符,如字母' I,l,o,q '和数字' 0,1 '。如果需要,可以添加一些中文或其他字符或公式。

$codeLen表示验证码的长度,通常为4位数。

3.2构造器,设置验证码字体,生成真彩色图像img

public function _ _ construct(){ $ this-font=ROOT _ PATH。/font/Chowderhead . TTF ';$ this-img=imagecreatetrue color($ this-width,$ this-heigh);}3.3从随机因素中随机选择4个字符作为$code验证码。

//生成随机代码私有函数create code(){ $ _ len=strlen($ this-charset)-1;for($ I=0;$ I $ this-CodeLen;$ I){ $这个-代码。=$this-charset[mt_rand(0,$ _ len)];} }3.4生成验证码的背景色。

//生成后台私有函数create BG(){ $ color=imagecolor locate($ this-img,mt _ rand (157,255),mt _ rand (157,255),mt _ rand (157,255));imagefiledrectangle($ this-img,0,$this-heigh,$this-width,0,$ color);}其中,mt_rand(157,255)旨在随机选择较浅的颜色。

3.5在图像上生成文本。

//生成文本私有函数create font(){ $ _ x=$ this-width/$ this-code len;$ _ y=$ this-heigh/2;for($ I=0;$ I $ this-CodeLen;$ I){ $ color=imagecolor allocate($ this-img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));imagettftext($this-img,$this-fontsize,mt_rand(-30,30),$_x * $i mt_rand(3,5),$_y mt_rand(2,4),$color,$this-font,$ this-code[$ I]);}}在图像上生成验证码字符,主要考虑字符在图像上的位置以及每个字符的颜色。

控制第n个字符的X轴位置=(图像宽度/验证码长度)* (n-1)个随机偏移量;其中n={D1.n}

控制第n个字符的Y轴位置=图像高度/2个随机偏移量;

Mt_rand(0,156)随机选择文本颜色,0-156旨在选择较深的颜色。

Mt_rand(-30,30)随机文本旋转。

3.6在图像上生成线条和雪花

//生成行,雪花私有函数create line(){ for($ I=0;$ i 15$ I){ $ color=imagecolor allocate($ this-img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));imageline($this-img,mt_rand(0,$this-width),mt_rand(0,$this-heigh),mt_rand(0,$this-width),mt_rand(0,$this-heigh),$ color);} for($ I=0;$ i 150$ I){ $ color=imagecolor allocate($ this-img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));imagestring($this-img,mt_rand(1,5),mt_rand(0,$this-width),mt_rand(0,$this-heigh),' # ',$ color);}}画线时取较深的色值,而画雪花时取较浅的色值,这样既能尽可能不影响人眼识别的验证码,又能干扰自动验证码识别机制。

3.7生成外部调用的验证码图片。

//生成公共函数doimg(){ $ this-createbg();//1.创建验证码背景$ this-create code();//2.生成随机代码$ this-create line();//3.生成线条和雪花$ this-createFont();//4.生成文本$ this-OutPut();//5.输出验证码图片}3.8完整代码:

?php/** *由PhpStorm创建。*用户:安迪*日期: 16-12-22 *时间:下午1:20 */class ValidateCode{ //随机因子private $ charset=' abcdefghjkmrstwxyzabbcdefgjkmrstwxyz 23456789 ';私有$代码;private $ codeLen=4;private $ width=130 private $ heigh=50 private $ img//图像私人$字体;//字体private $ font size=20 public function _ _ construct(){ $ this-font=ROOT _ PATH ./font/Chowderhead。' TTF ';$ this-img=imagecreatetrue color($ this-width,$ this-heigh);} //生成随机码私有函数create code(){ $ _ len=strlen($ this-charset)-1;对于($ I=0;$ I $ this-CodeLen;$ I){ $这个-代码=$this-charset[mt_rand(0,$ _ len)];} } //生成背景私有函数createBg(){ $ color=imagecolor allocate($ this-img,mt_rand(157,255),mt_rand(157,255),mt_rand(157,255));imagefiledrectangle($ this-img,0,$this-heigh,$this-width,0,$ color);} //生成文字私有函数create font(){ $ _ x=$ this-width/$ this-codeLen;$ _ y=$ this-heigh/2;对于($ I=0;$ I $ this-CodeLen;$ I){ $ color=image color allocate($ this-img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));imagettftext($this-img,$this-fontsize,mt_rand(-30,30),$_x * $i mt_rand(3,5),$_y mt_rand(2,4),$color,$this-font,$ this-code[$ I]);} } //生成线条,雪花私有函数创建行(){ for($ I=0;$ I 15 $ I){ $ color=image color allocate($ this-img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));imageline($this-img,mt_rand(0,$this-width),mt_rand(0,$this-heigh),mt_rand(0,$this-width),mt_rand(0,$this-heigh),$ color);} for($ I=0;$ I 150 $ I){ $ color=imagecolor allocate($ this-img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));imagestring($this-img,mt_rand(1,5),mt_rand(0,$this-width),mt_rand(0,$this-heigh),' # ',$ color);} } //输出图像私有函数OutPut(){ header(' Content-Type : image/png ');image png($ this-img);图像销毁($ this-img);} //对外生成公共函数DoImg(){ $ this-CreateBG();//1.创建验证码背景$ this-create COde();//2.生成随机码$ this-create line();//3.生成线条和雪花$ this-createFont();//4.生成文字$ this-OutPut();//5.输出验证码图像} //获取验证码公共函数GetCode(){ return strtolow($ this-code);}}4.测试

测试代码:

?php/** *由PhpStorm创建。*用户:安迪*日期: 16-12-22 *时间:下午1:20 */define('ROOT_PATH ',dir name(_ _ FILE _ _));需要一次ROOT_PATH ./包含/验证代码。上课。PHP ';$ _ VC=新的验证代码();echo $ _ VC-DoImg();生成验证码:

5.应用

labelimg src='http:/config/代码。PHP ' onclick=' JavaScript :这个。' src='./config/code.php?TM='数学。' random();'//标签上面onclick代码是点击验证码图片,能自动刷新验证码。

code.php:

?php/** *由PhpStorm创建。*用户:安迪*日期: 16-12-22 *时间:下午3:43 */需要substr(dirname(_ FILE _ _),0,-7)./init。Inc . PHP ';$ _ VC=新的验证代码();echo $ _ VC-DoImg();$ _ SESSION['验证代码']=$ _ VC-GetCode();有关应用的完整代码可以从https://git.oschina.net/andywww/myTest的CMS1.0文件里下载。

6.小结

在独立测试过程,没发现什么问题;但应用到项目的时候,刚开始发现无法生成验证码图片,网上找了一下,有的说是在outPut()函数中,

在标题(“内容类型:图像/png”);这行代码前面增加了一行ob_clean()代码,可以解决验证码这块问题。虽然此方法简单,但这可能会引起其他缓冲数据问题,因为db_clean()功能就是丢弃输出缓冲区中的内容。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

版权声明:PHP验证码类ValidateCode分析是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。