宝哥软件园

PHP验证码类文件及调用方式代码详解

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

代码如下所示:

//验证码类类验证代码{ private $ charset=' abcdefghkmnprstwoxyzabbcdefghkmnprstwoxyz 23456789 ';//随机因子私有$代码;//验证码private $代码len=4;//验证码长度private $ width=130//宽度私$身高=50;//高度private $ img/图形资源句柄私人$字体;//指定的字体private $ fontsize=20/指定字体大小private $ fontcolor//指定字体颜色//构造方法初始化public function _ _ construct(){ $ this-font=dirname(_ _ FILE _ _)./font/大象。' TTF ';//注意字体路径要写对,否则显示不了图片} //生成随机码私有函数create code(){ $ _ len=strlen($ this-charset)-1;对于($ I=0;$ I $这个-代码len$ I){ $这个-代码=$this-charset[mt_rand(0,$ _ len)];} } //生成背景私有函数create BG(){ $ this-img=imagecreatetrue color($ this-width,$ this-height);$ color=image color allocate($ this-img,mt_rand(157,255),mt_rand(157,255),mt_rand(157,255));imagefiledrectangle($ this-img,0,$this-height,$this-width,0,$ color);} //生成文字私有函数createFont(){ $ _ x=$ this-width/$ this-code len;对于($ I=0;$ I $这个-代码len$ I){ $ this-font 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(1,5),$this-height/1.4,$this-fontcolor,$this-font,$ this-code[$ I]);} } //生成线条、雪花私有函数createLine() { //线条对于($ I=0;$ i6 $ 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-height),mt_rand(0,$this-width),mt_rand(0,$this-height),$ color);} //雪花对于($ I=0;100美元;$ 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-height),' *,$ color);} } //输出私有函数OutPut(){ header(' Content-type : image/png ');image png($ this-img);图像销毁($ this-img);} //对外生成公共函数doi mg(){ $ this-createBg();$ this-create COde();$ this-create line();$ this-createFont();$ this-OutPut();} //获取验证码公共函数GetCode(){ return strtolow($ this-code);} }

使用方法:

1、先把验证码类保存为一个名为ValidateCode.class.php的文件;

2、新建一个名为captcha.php的文件进行调用该类;

captcha.php

3、引用到页面中,代码如下:

4、一个完整的验证页面,代码如下:

?PHP session _ start();//在页首先要开启会话,//error _ reporting(2047);session _ destroy();//将会议去掉,以每次都能取新的会议值;//用seesion效果不错,也很方便?htmlheadtitlesession图片验证实例/title style type=' text/CSS ' #登录p { margin-top : 15px;线高: 20pxfont-size : 14px字体粗细:粗体;} #登录img { cursor:pointer}表单{ margin-left :20 px;}/style/head body表单id=' log in ' action=' method=' post ' p此例为会议验证实例/ppspan验证码:/span put type=' text ' name=' validate ' value=' size=10 img title='点击刷新src='http:/验证码。PHP ' align=' abbottom ' onclick=' this。src='验证码。PHP?数学。random();/img/ppin put type=' submit '/p/form?php/打印上一个会议;//回声'上一个会话:""。$ _ SESSION[' authnum _ SESSION ']" br ";$ validate=if(isset($ _ POST[' validate ']){ $ validate=$ _ POST[' validate '];"回声"您刚才输入的是:'.$_POST['验证']'英国铁路公司状态:';if($validate!=$ _ SESSION[' authnum _ SESSION ']){//判断会议值与用户输入的验证码是否一致;"回声"字体颜色=红色输入有误/font ';} else { echo '字体颜色=绿色通过验证/font ';}} ?总结

以上是边肖介绍的PHP验证码文件和调用方法的详细说明,希望对大家有所帮助。如果你有任何问题,请给我留言,边肖会及时回复你。非常感谢您对我们网站的支持!

更多资讯
游戏推荐
更多+