本文实例讲述了thinkPHP中验证码的简单实现方法。分享给大家供大家参考,具体如下:
运行效果图如下:
1.php端生成验证码函数
公共函数verify(){ //验证码导入(' @ .乌提尔。图像');image : buildimageverify(4,1,' png ',40,20,' verify ');}/*** 生成图像验证码* @static* @access public* @param字符串$长度位数* @param字符串$模式类型* @param字符串$类型图像格式* @param字符串$宽度宽度* @param字符串$高度高度* @返回字符串*/static函数buildImageVerify($长度=4,$mode=1,$type='png ',$width=48,$height=22,$ verify name=' verify '){ import(' ORG .乌提尔。字符串');$ rand val=字符串:3360 rand string($ length,$ mode);会话($verifyName,MD5($ rand val));$width=($length * 10 10) $width?$长度* 10 10 : $宽度;if ($type!=' gif ' function _ exists(' imagecreatetrue color '){ $ im=imagecreatetrue color($ width,$ height);} else { $im=imagecreate($width,$ height);} $r=Array(225,255,255,223);$g=Array(225,236,237,255);$b=数组(225,236,166,125);$key=mt_rand(0,3);$ back color=image color allocate($ im,$r[$key],$g[$key],$ b[$ key]);//背景色(随机)$ border color=image color allocate($ im,100,100,100);//边框色imagefiledrectangle($ im,0,0,$width - 1,$height - 1,$ backColor);imagerectangle($im,0,0,$width - 1,$height - 1,$ borderColor);$ StringColor=imagecolorallocate($ im,mt_rand(0,200),mt_rand(0,120),mt_rand(0,120));//干扰对于($ I=0;10美元;$i ) { imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$ stringColor);} for($ I=0;$ i 25$i ) { imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$ string color);} for($ I=0;一美元长度;$i ) { imagestring($im,5,$i * 10 5,mt_rand(1,8),$randval{$i},$ StringColor);} Image:output($im,$ type);}生成图片的同时,传入到会议中。
2.页面端
img id=' verifyImg ' src=' http : { sh :3360 u(' Agent/log in/verify ')} ' onClick=' change verify()' title='点击刷新验证码//div直接科学研究委员会中调用。
点击触发改变。
函数change verify(){ verify URL=' { sh : u('代理/登录/验证')} ';$('#verifyImg ').attr('src ',verifyURl);返回false}3。后台验证,对比邮政字段与会议中的验证码是否一致。
if($_SESSION['验证']!=md5($_POST['验证]){ $ this-错误('验证码错误!');}更多关于thinkPHP相关内容感兴趣的读者可查看本站专题: 《ThinkPHP入门教程》 、 《thinkPHP模板操作技巧总结》 、 《ThinkPHP常用方法总结》 、 《codeigniter入门教程》 、 《CI(CodeIgniter)框架进阶教程》 、 《Zend FrameWork框架入门教程》 、 《smarty模板入门基础教程》 及《PHP模板技术总结》 。
希望本文所述对大家基于ThinkPHP框架的服务器端编程语言(专业超文本预处理器的缩写)程序设计有所帮助。