手机版

php生成图片验证码-附五种验证码

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

以前输出验证码的时候用过一个方法,在前台用射流研究…生成验证码字符串,再传递到后台用服务器端编程语言(专业超文本预处理器的缩写)输出验证码图像。这样在验证时就不需要使用$_SESSION传递验证码的值,直接用射流研究…比较生成的字符串和输入的字符串是否相等即可。

本文以实例演示5种验证码,并介绍生成验证码的函数PHP。生成验证码的原理:通过权志龙库,生成一张带验证码的图片,并将验证码保存在会议中。

1、超文本标记语言

5中验证码超文本标记语言代码如下:

div class='demo' h31、数字验证码/h3 p验证码:input type=' text ' class=' input ' id=' code _ num ' name=' code _ num ' maxlength=' 4 '/img src=' http : code _ num。PHP ' id=' getcode _ num ' title='看不清,点击换一张align=' ABS middle '/p pinput type=' button ' class=' BTN ' id=' chk _ num ' value='提交//p /div div class='demo' h32、数字字母验证码/h3 p验证码:input type=' text ' class=' input ' id=' code _ char ' maxlength=' 4 '/img src=' http : code _ char。PHP ' id=' getcode _ char ' title='看不清,点击换一张align=' ABS middle '/p pinput type=' button ' class=' BTN ' id=' chk _ char ' value='提交//p /div div class='demo' h33、中文验证码/h3 p验证码:input type=' text ' class=' input ' id=' code _ zh ' maxlength=' 4 '/img src=' http : code _ zh。PHP ' id=' getcode _ zh ' title='看不清,点击换一张align=' ABS middle '/p pinput type=' button ' class=' BTN ' id=' chk _ zh ' value='提交//p /div div class='demo' h34、仿谷歌验证码/h3 p验证码:输入类型=' text ' class=' input ' id=' code _ gg ' maxlength=' 4 '/img src=' http : code _ gg。PHP ' id=' getcode _ gg ' title='看不清,点击换一张align=' ABS middle '/p pinput type=' button ' class=' BTN ' id=' chk _ gg ' value='提交//p /div div class='demo'h35、算术验证码/h3 p验证码:input type=' text ' class=' input ' id=' code _ math ' maxlength=' 4 '/img src=' http : code _ math。PHP ' id=' getcode _ math ' title='看不清,点击换一张align=' ABS middle '/p pinput type=' button ' class=' BTN ' id=' chk _ math ' value='提交//p/div2、js验证

$(函数(){ $('#getcode_num ')).单击(函数(){ //数字验证$(这个)。attr('src ',' code_num.php '?数学。random());});$('#chk_num ').单击(函数(){ var code _ num=$(' # code _ num ')).val();$.post('chk_code.php?act=num ',{ code: code_num },函数(msg) { if (msg==1) { alert('验证码正确!');} else { alert('验证码错误!');} });});//数字字母验证

$('#getcode_char ').单击(function() { $(this)).attr('src ',' code_char.php '?数学。random());});$('#chk_char ').单击(函数(){ var code _ char=$(' # code _ char ')).val();$.post('chk_code.php?act=char ',{ code: code_char },function(msg) { if (msg==1) { alert('验证码正确!');} else { alert('验证码错误!');} });});//中文验证码

$('#getcode_zh ').单击(function() { $(this)).attr('src ',' code_zh.php '?数学。random());});$('#chk_zh ').单击(函数(){ var code _ zh=escape($(' # code _ zh ')).val());$.post('chk_code.php?act=zh ',{ code: code_zh },函数(msg) { if (msg==1) { alert('验证码正确!');} else { alert('验证码错误!');} });});//谷歌验证

$('#getcode_gg ').单击(function() { $(this)).attr('src ',' code_gg.php '?数学。random());});$('#chk_gg ').单击(function(){ var code _ gg=$(' # code _ gg ').val();$.post('chk_code.php?act=gg ',{ code: code_gg },函数(msg) { if (msg==1) { alert('验证码正确!');} else { alert('验证码错误!');} });});//算术验证

$('#getcode_math ').单击(function() { $(this)).attr('src ',' code_math.php '?数学。random());});$('#chk_math ').单击(function(){ var code _ math=$(' # code _ math ').val();$.post('chk_code.php?act=math ',{ code: code_math },函数(msg) { if (msg==1) { alert('验证码正确!');} else { alert('验证码错误!');} });});});3、PHP生成验证码

session _ start();getCode(4,60,20);函数getCode($num,$w,$ h){ $ code=' ';对于($ I=0;$ i $ num$i ) { $code .=rand(0,9);} //4位验证码也可以用兰特(1000,9999)直接生成//将生成的验证码写入会话,备验证时用$ _ SESSION[' helloweba _ num ']=$ code;//创建图片,定义颜色值标题(“内容类型:图像/巴布亚新几内亚”);$im=imagecreate($w,$ h);$black=imagecolorallocate($im,0,0,0);$gray=imagecolorallocate($im,200,200,200);$ BG color=image color allocate($ im,255,255,255);//填充背景imagefill($im,0,0,$ gray);//画边框imagerectangle($im,0,0,$w-1,$h-1,$ black);//随机绘制两条虚线,起干扰作用$style=array ($black,$black,$black,$gray,$gray,$ gray);imagesetstyle($im,$ style);$y1=兰特(0,$ h);$y2=兰特(0,$ h);$y3=兰特(0,$ h);$y4=兰特(0,$ h);imageline($im,0,$y1,$w,$y3,IMG _ COLOR _ style);imageline($im,0,$y2,$w,$y4,IMG _ COLOR _ style);//在画布上随机生成大量黑点,起干扰作用;对于($ I=0;$ i 80$i ) { imagesetpixel($im,rand(0,$w),rand(0,$h),$ black);} //将数字随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成$strx=兰特(3,8);对于($ I=0;$ i $ num$i ) { $strpos=rand(1,6);imagestring($im,5,$strx,$strpos,substr($code,$i,1),$ black);$strx=兰特(8,12);} image png($ im);//输出图片图像销毁($ im);//释放图片所占内存}以上内容就是服务器端编程语言(专业超文本预处理器的缩写)生成图片验证码-附五种验证码的全部内容,希望大家喜欢。

版权声明:php生成图片验证码-附五种验证码是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。