继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

PHP+AJAX实现简单验证码程序实例

ios开发零基础入门
关注TA
已关注
手记 265
粉丝 20
获赞 110

最新PHP+AJAX实现简单验证码程序实例

以下是三零网为大家整理的最新PHP+AJAX实现简单验证码程序实例的文章,希望大家能够喜欢!

这是一个项目中用到的。。。分享大家看看吧!


vcode.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; 
}      
?>

 


 

loginform.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>

 


 

vcode.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;
    }
          }
}
}


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP