PHP+AJAX 实现简单验证码示例
内容摘要
这篇文章主要为大家详细介绍了PHP+AJAX 实现简单验证码示例,具有一定的参考价值,可以用来参考一下。
这是一个项目中用到的。。。分享大家看看吧!vcode.php,经测试代码如下:
<?
这是一个项目中用到的。。。分享大家看看吧!vcode.php,经测试代码如下:
<?
文章正文
这篇文章主要为大家详细介绍了PHP+AJAX 实现简单验证码示例,具有一定的参考价值,可以用来参考一下。
这是一个项目中用到的。。。分享大家看看吧!vcode.php,经测试代码如下:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <code class = "php" > <?php session_start(); //开启session功能 header( "Cache-Control: no-cache, must-revalidate" ); $im = imagecreate(60,30); //定义图片宽度和高度 $vcode =getVCode(); //获取要显示的字符 $bg = imagecolorallocate( $im , 255, 255, 255); //定义图片背景 $txt = imagecolorallocate( $im , rand(0,255), rand(0,255), rand(0,255)); //定义要显示字符的颜色 imagestring( $im , 8, 0, 0, $vcode , $txt ); //写入字符串到图片 header(Content-type: image/jpeg); //定义Content-type imagejpeg( $im ); //以JPEG格式显示图片 $_SESSION [vcode]= $vcode ; //写入SESSION function getVCode(){ //随机生成用户指定个数的字符串 $codenum =4; $checkcode = "" ; $string = "" ; //要显示的可选字符串,请自行定义; for ( $i =0; $i < $codenum ; $i ) { $number =rand(0,2); switch ( $number ){ //根据可选字符串可灵活定义; case 0 : $rand_number =rand(0,10); break ; case 1 : $rand_number =rand(11,36); break ; case 2 : $rand_number =rand(37,62); break ; } $code = substr ( $string , $rand_number ,1); $checkcode = $checkcode . $code ; } return $checkcode ; } ?> </code> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <code class = "html" > <!--详细信息--> <form name= "loginform" > <table class = "dtable" > <tr> <td width= "100" > 用户名 </td><td><input class = "txtbox" name= "loginname" type=text size= "30" /></td> </tr> <tr> <td width= "100" > 密码 </td><td><input class = "txtbox" name= "loginpwd" type=password size= "30" /></td> </tr> <tr> <td width= "100" > 验证码 </td> <td><input class = "txtbox" name= "loginvcode" type=text size= "10" /> <img id= "vcode" src= "vcode.php" alt= "验证码" align= "absmiddle" /> <a href= "javascript:getVCode();" >换一张</a></td> </tr> </table> <table> <tr><td colspan= "2" > <input class = "btn" name= "ok" type= "button" value= "登录" onclick= "setType('usr');usrVCode();" > <input class = "btn" name= "reset" type= "reset" value= "重写" > <input class = "btn" name= "exit" type= "button" value= "退出" onclick= "Hide();" > </td></tr> </table> <table> <tr><td colspan= "2" > 还没有注册? <a href= "javascript:setType('usr');Show('0','addform');" >马上注册</a> 忘记密码? <a href= "javascript:setType('usr');Show('0','pwdform');" >取回密码</a> </td></tr> </table> </form> </code> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | <code class = "js" > //该函数用来获取验证码 function getVCode() { var vcode=document.getElementById( 'vcode' ); vcode.src = 'vcode.php?nocache=' + new Date ().getTime(); } //该函数用来验证验证码 function usrVCode() { if (!checkLogin()) return false; var loginvcode=document.loginform.loginvcode.value; var xmlhttp1=createAjax(); var data= '&loginvcode=' +loginvcode; if (xmlhttp1) { var state=document.getElementById( 'state' ); xmlhttp1.open( 'get' ,? do =vcodedo'+data,true); xmlhttp1.send(null); xmlhttp1.onreadystatechange= function () { if (xmlhttp1.readyState==4 && xmlhttp1.status==200) { setTimeout( "state.style.display = 'none';" ,1000); var myres=xmlhttp1.responseText; var result=(myres==1)? "恭喜您,验证码输入正确!" : "很抱歉,验证码输入错误!" ; if (myres==0)alert(result); if (myres==1)usrLogin(); } else { state.style.display = "" ; state.style.left=(document.body.offsetWidth-350)/2; state.style.top=(document.body.offsetHeight-235)/2+document.body.scrollTop; } } } } /*** 来自:php教程(www.idcnote.com) ***/ </code> |
注:关于PHP+AJAX 实现简单验证码示例的内容就先介绍到这里,更多相关文章的可以留意
代码注释