手机版

一个漂亮的服务器端编程语言(专业超文本预处理器的缩写)验证码类(分享)

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

直接上代码:复制代码代码如下://验证码类类验证代码{ 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复制代码代码如下: session _ start();要求/validatecode。上课。PHP ';//先把类包含进来,实际路径根据实际情况进行修改$ _ VC=新的验证代码();//实例化一个对象$ _ VC-doi mg();$ _ SESSION[' authnum _ SESSION ']=$ _ VC-GetCode();//验证码保存到会议中3、引用到页面中,代码如下:复制代码代码如下:img title='点击刷新src='http:/验证码。PHP ' align=' ABS bottom ' onclick=' this。src='验证码。PHP?数学。random();"/img4 ,"一个完整的验证页面,代码如下:复制代码代码如下: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;}/样式/头体表单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 ';}} ?完整演示下载:演示

版权声明:一个漂亮的服务器端编程语言(专业超文本预处理器的缩写)验证码类(分享)是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。