php验证码实现代码(3种)
越来越多的验证码以表单的形式实现,但是用js写验证码总是不方便,所以我学习了用php实现的验证码。
嗯,没什么可做的,但是我不想浪费时间,所以我学习了php来实现验证码。俗话说,技术好的人不压自己。而且还可以封装成函数,非常方便以后使用,但是现在不封装了。现在来说说简单的纯数字验证码。如果你是初学者,建议按照我的代码中的注释//数字一步一步来。最简单的方法是复制整个代码。创建一个新的验证码. php:
?php //10设置的Session必须在脚本session_start()的顶部;$ image=imagecreatetrue color(100,30);//1设置验证码图片大小的功能//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);//10设置变量$ captcha _ code=//7为($i=0)生成随机数;$ i4$i ){ //设置字体大小$ font size=6;//设置字体颜色,随机颜色$ font color=imagecolor locate($ image,rand (0,120),rand (0,120),rand (0,120));//0-120深色//设置数字$fontcontent=rand(0,9);//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));//设置行、二行和一行图像行($ 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)的图形函数销毁;然后是静态页面的代码:index.html
!doctype html html head meta http-equiv=' Content-Type ' Content=' text/html;Charset=UTF-8' title确认验证码/title/头体形式方法=' post' action='。/form . PHP ' p验证代码:img ID=' captcha _ img '边框=' 1' src='。/验证码. php?r=?PHP echo rand();'style=' width:100pxheight :30 px '/a href=' JavaScript : void(0)' ' onclick=' document . getelementbyid(' captcha _ img ')。src='。/验证码. php?R=' Math.random()'换一个?/a/p p请输入验证码:输入类型=' text ' name=' authcode ' value=' '//p pin put type=' submit ' value=' submit ' style=' padd :6 px 5px;'//p/body/html从index.html可以看到,提交的表格是给form.php的,所以肯定有判断form.php的代码:
?php头(' Content-type : text/html;charset=utf-8 ');//设置头信息//isset()检测变量是否设置为if(isset($ _ request[' authcode '])){ session _ start();///strtolow()小写函数if(strlower($ _ request[' authcode '])=$ _ session[' authcode ']){//跳转到echo '脚本语言=\ ' JavaScript \echo“document . location=\”。/form . PHP \ ' ';echo '/script ';}else{ //提示并跳转页面echo ' script language=\ ' JavaScript \ ';回声“alert(!idspnonenote)输入错误。');';echo“document . location=\”。/form . PHP \ ' ';echo '/script ';} exit();}显示页面如下:
那么,随着纯数字的实现,给英语加上数字应该不难。废话少说,拉代码。
?php //10设置的Session必须在脚本session_start()的顶部;$ image=imagecreatetrue color(100,30);//1设置验证码图片大小的功能//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);//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=' abcdefghigkmnpqrstuvwxy 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));//设置行、二行和一行图像行($ 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的。
?php //11设置的Session必须在脚本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 $strdb=str_split($str,3)中是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 $captcha_code的字符串中。=$ cn/* imagettftext (resource $ image,float $ size,float $ angle,int $ x,int $ y,int $ color,string $ fontfile,string $ text)屏幕,大小,角度,坐标,颜色,字体路径,Text string 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));//设置线条,二行和一行图像线条($ 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验证码实现代码(3种)是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。