教你服务器端编程语言(专业超文本预处理器的缩写)如何实现验证码
验证码在表单实现越来越多了,但是用射流研究…的写的验证码,总觉得不方便,所以学习了下服务器端编程语言(专业超文本预处理器的缩写)实现的验证码。好吧,其实是没有事情干,但是又不想浪费时间,所以学习了下服务器端编程语言(专业超文本预处理器的缩写)实现验证码。正所谓,技多不压身。而且,也可以封装成一个函数,以后使用的时候也是很方便的,当然现在未封装。
现在来说说简单的纯数字验证码吧。
如果是初学者,建议按照我代码的注释//数字一步步来。最简单的方法,还是把整个代码复制走了。
新建一个验证码。php:
php //10设置会话,必须处于脚本最顶部session _ start();$ image=imagecreatetrue color(100,30);//1设置验证码图片大小的函数//5设置验证码颜色imagecolorable(int im、int red、int green、int blue);$ BG color=image color allocate($ image,255,255,255);//#ffffff //6区域填充int imagefill(int im,int x,int y,int col) (x,y)所在的区域着色上校表示欲涂上的颜色imagefill($image,0,0,$ bgcolor);//10设置变量$验证码=//7生成随机数字对于($ I=0;$ i4$i ){ //设置字体大小$ font size=6;//设置字体颜色,随机颜色$ font color=image color allocate($ image,rand(0,120),rand(0,120),rand(0,120));//0-120深颜色//设置数字$fontcontent=rand(0,9);//10.=连续定义变量$验证码_代码=$ fontcontent//设置坐标$x=($i*100/4)兰特(5,10);$y=兰特(5,10);imagestring($image,$fontsize,$x,$y,$fontcontent,$ font color);} //10存到SESSION $ _ SESSION[' auth code ']=$ captcha _ code;//8增加干扰元素,设置雪花点对于($ I=0;$ i200$i ){ //设置点的颜色,50-200颜色比数字浅,不干扰阅读$ point color=image color allocate($ image,rand(50,200),rand(50,200),rand(50,200));//imagesetpixel —画一个单一像素imagesetpixel($image,rand(1,99),rand(1,29),$ point color);} //9增加干扰元素,设置横线对于($ I=0;$ i4$i ){ //设置线的颜色$ line color=image color allocate($ image,rand(80,220),rand(80,220),rand(80,220));//设置线,两点一线imageline($image,rand(1,99),rand(1,29),rand(1,99),rand(1,29),$ linecolor} //2设置头部,图像/png标题('内容-类型:图像/png ');//3imagepng()建立巴布亚新几内亚图形函数image png($ image);//4imagedestroy()结束图形函数销毁$ image destroy($ image);接着就是静态页的代码了:index.html
doctype html html head meta http-equiv=' Content-Type ' Content=' text/html;charset=UTF-8 '标题确认验证码标题标题正文形式方法='post '操作=' ./form.php' p验证码: img id='captcha_img '边框='1' src=' ./验证码。php?r=回声兰德();'style=' width :100 px h8 :30 px '/a href=' JavaScript:void(0)' onclick=' document。getelementbyid(' captcha _ img ').src=' ./验证码。php?r=' Math.random()'换一个?美联社请输入验证码:输入类型=' text '名称=' authcode '值=' '/p pinput类型=' submit '值='提交style=' padd :6 px 5px '/p/body/html从index.html可以看到,提交的表单是到form.php的,所以还要有一个判断的form.php代码:
服务器端编程语言(Professional Hypertext Preprocessor的缩写)头('内容类型:文本/html;charset=utf-8 ');//设置头部信息//isset()检测变量是否设置if(isset($ _ REQUEST[' auth code ']){ session _ start();//strtolow()小写函数if(strtolow($ _ REQUEST[' auth code '])=$ _ SESSION[' auth code ']){//跳转页面“回声”脚本语言=\ ' javascript \echo "文档。位置=\ " ./form。PHP \echo '/script ';}else{ //提示以及跳转页面“回声”脚本语言=\ ' javascript \echo 'alert('输入错误!');';回声”文档。位置=\ " ./form。PHP \echo '/script ';} exit();}显示页面如下:
那么,随着纯数字的实现,用英语添加数字应该不难。要修改的代码只是把//7生成的随机数在captcha.php改成//7生成的随机字母和数字。如果你真的很可爱,只要修改一下这些文字,觉得可以实现,那么恭喜你,你会一直幸福的。大脑残疾的孩子要快乐得多。
废话少说,拉代码。
php //10设置的Session必须在脚本session_start()的顶部;$ image=imagecreatetrue color(100,30);//1功能设置验证码图片的大小//5设置验证码颜色imagecolorclocate(int im、int red、int green、int blue);$ BG color=imagecolor allocate($ image,255,255,255);//#ffffff //6填充int imagefill (int im,int x,int y,int col) (x,y)所在的区域,col表示要绘制的颜色imagefill($image,0,0,$ bgcolor);//10设置变量$ captcha _ code=//7为($i=0)生成随机字母和数字;$ i4$i ){ //设置字体大小$ font size=8;//设置字体颜色,随机颜色$ font color=imagecolor locate($ image,rand (0,120),rand (0,120),rand (0,120));//0-120深色//设置需要随机取的值,去掉容易出错的值如0和o $ data=' abcdefghigkmnppqrstuvwxy 3456789 ';//取出值,字符串截取方法strlen得到字符串长度$ fontcontent=substr ($ data,rand (0,strlen ($ data)),1);//10.=连续定义变量$captcha_code。=$ fontcontent//设置坐标$x=($i*100/4) rand(5,10);$y=兰特(5,10);imagestring($image,$fontsize,$x,$y,$fontcontent,$ font color);} //10保存到session $ _ session[' authcode ']=$ captcha _ code;//8添加干扰元素,设置雪花点为($ I=0;$ i200$i ){ //设置点的颜色,50-200比数字浅,不妨碍读取$ point color=imagecolor locate($ image,rand (50,200),rand (50,200),rand (50,200));//imageset pixel-绘制单像素imagesetpixel ($ image,rand (1,99),rand (1,29),$ point color);} //9添加干扰元素,设置水平线为($ I=0;$ i4$i ){ //设置行的颜色$ line color=imagecolor locate($ image,rand (80,220),rand (80,220),rand (80,220));//设置行,2.1行图像行($ image,rand (1,99),rand (1,29),rand (1,99),rand (1,29),$ line color);} //2设置头,image/png头(' content-type : image/png ');//3imagepng()创建png图形函数imagepng($ image);//4imagedestroy()结束对图形函数$ image destroy($ image)的销毁;另外两页不允许修改。
总的来说,现在已经足够了。但就像动漫一样,总会有特别的东西。
然后,让我们对汉字有一个特殊的解释。其实我打算把汉字验证码放到我的毕业设计里。虽然滑动验证码现在很流行,但是我不是专门研究js的。
而且,你也可以告诉被调查者的老师,我们验证码不需要任何材料,甚至图片都是生成的。我们用自己的知识去装13,并没有这回事。
Session是在php //11中设置的,必须在脚本session_start()的顶部;//1 $ image=image create true color(200,60)用于设置验证码图片的大小;//5设置验证码颜色imagecolorlocate (int im、int red、int green、int blue);$ BG color=imagecolor allocate($ image,255,255,255);//#ffffff //6填充int imagefill (int im,int x,int y,int col) (x,y)所在的区域,col表示要绘制的颜色imagefill($image,0,0,$ bgcolor);//7 Set ttf font $fontface='FZYTK。' TTF ';//7设置字体,实现简单的数字储备$str='天地无情,待万物如狗,圣人无情,待人如狗。这句话经常出现在指责暴君、暴政、残暴,把所有的事情都当成卑微的猪和狗。上面那些所谓的圣人也没什么不同。他们也把我们的人看得不如猪狗,但他们所采取的解释是,他们在感情上没有习惯于平等对待一切,圣人在感情上也没有习惯于平等对待人。女主角深情地看着对方说:“牵着你的手”“陪着老婆婆带着儿子”。她含泪羞愧地回答:“恨”。这样的剧情我们看过很多吗?但我们来看看这句话的原句“死富子富”。不,我和我的儿子住在一起,并以富有的方式变老。不,我相信。我的意思是士兵之间的协议说他们想一起死。现在我同意的人都走了。我该如何生活?江湖上兄弟战友之间赤裸裸的友情?//str_split()将字符串剪切成数组,在utf_8中,一个汉字是3个字符。$strdb=str_split($str,3);//11 $ captcha _ code=' ';//8为($i=0)生成随机人;$ i4$i ){ //设置字体颜色,随机颜色$ font color=imagecolor locate($ image,rand (0,120),rand (0,120),rand (0,120));//0-120种深色//随机选择中文$in=rand(0,count($ strdb));$ cn=$ strdb[$ in];//将中文记录到要保存在session $验证码中的字符串中。=$ cn/* imagettftext (resource $ image,float $ size,float $ angle,int $ x,int $ y,int $ color,string $ font file,string $ text)屏幕,大小,角度,坐标,颜色,字体路径,Text mt_rand()生成更好的随机数,比rand () */imagettftext ($ image,mt _ rand (20,24),mt _ rand (-60,60),(40 * $ I 20),mt _ rand (30,35),} //11保存到session $ _ session[' authcode ']=$ captcha _ code;//9添加干扰元素并设置点为($ I=0;$ i200$i ){ //设置点的颜色,50-200比数字浅,不妨碍读取$ point color=imagecolor locate($ image,rand (50,200),rand (50,200),rand (50,200));//imageset pixel-绘制单像素imagesetpixel ($ image,rand (1,199),rand (1,59),$ point color);} //10添加干扰元素,设置线为($ I=0;$ i4$i ){ //设置行的颜色$ line color=imagecolor locate($ image,rand (80,220),rand (80,220),rand (80,220));//设置行,2.1行图像行($ image,rand (1,199),rand (1,59),rand (1,199),rand (1,59),$ line color);} //2设置头,image/png头(' content-type : image/png ');//3imagepng()创建png图形函数imagepng($ image);//4imagedestroy()结束对图形函数$ image destroy($ image)的销毁;效果图如下。
这就是本文的全部内容,将帮助您实现php数字验证码、php字母验证码和php汉字验证码,希望对您的学习有所帮助。
版权声明:教你服务器端编程语言(专业超文本预处理器的缩写)如何实现验证码是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。