ajax验证码

前端之家收集整理的这篇文章主要介绍了ajax验证码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

vcode.PHP

<?PHP
/*
 * #
 * 开启session
 * 随机生成4位数的验证码(包涵字母或数字)
 *$te
 *$im
 *$bg背景色
 * #*/
session_start();
for($i=0;$i<4;$i++){$rand.=dechex(rand(1,15));}
$_SESSION[check_pic]=$rand;
$im=imagecreatetruecolor(50,18);
$bg=imagecolorallocate($im,0);
$te=imagecolorallocate($im,255,255);
imagestring($im,rand(1,3),20),rand(0,5),$rand,$te);
Header("Content-type: image/PNG");
imagejpeg($im);
//echo $_SESSION[check_pic];
?>

index.PHP
<?PHP
//导入基本配置文件
header('Content-Type:text/html;charset=GB2312');
include_once("admin_global.PHP");
/*
 * 如果post会来的<用户名>和<密码>不为空,
   那么进行判断用户是否为管理员
*/
if(!empty($_POST[username])&& !empty($_POST[password])) $db->Get_user_login($_POST[username],$_POST[password]);
?>
<html>
<script language="JavaScript" type="text/javascript" src="ajaxs/vcode_js.js"></script>
<head>
<Meta http-equiv='Content-Type' content='text/html; charset=gb2312'>
<Meta name='Author' content='Alan'>
<link rev=MADE href='mailto:haowubai@hotmail.com'><title>后台管理</title>
<link rel='stylesheet' type='text/css' href='images/private.css'>
<script> if(self!=top){ window.open(self.location,'_top'); } </script>
</head>

<body>

<br><br><br>

<form action="" method="post">
<table border=0 cellspacing=1 align=center class=form>
<tr>
<th colspan="2">用户登录</th>
</tr>
<tr>
<td align="right">登录用户:</td>
<td><input type="text" name="username" value="" size="20" maxlength="40"/>  </td>
</tr>
<tr>
<td align="right">登录密码:</td>
<td><input type="password" name="password" value="" size="20" maxlength="40"/>  </td>
</tr>
<tr>
<td align="right">验证码:</td>
<td><input type="text" name="yzm" value="" size="20" maxlength="40"  onkeyup="javascript:vcode_check('yzm')"/>
<img id=vcode_img src="ajaxs/vcode.PHP" alt="看不清?点击换一张" align="absmiddle" onclick="javascript:this.src='ajaxs/vcode.PHP?image='+new Date().getTime();"/>
<span id=vcode_tx></span>
</td>
</tr>
<tr>
<td colspan="2" align="center" height='30'>
<input type="submit" name="update" value=" 登录 " onclick="return check();"/>
</td>
</form>

</tr>
</table>
</body></html>

vcode_check.PHP
<?PHP
/*
 * Created on 2013-2-16
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
session_start();
//include("../conn/connDB.PHP");
header('Content-Type:text/html;charset=GB2312');
$vocode=$_GET["vocode"];

if(!empty($vocode)){
	if($vocode==$_SESSION[check_pic]){
		//填写正确
	/*echo "<span style='width:27px;  height:19px;  background:url(images/icon.png) repeat-y; background-position:-25px 0px
'></span>";*/
	echo "2";
	}else{//echo "<font color=red> 验证码有误:".$_SESSION[check_pic]."</font>";
		/*echo "<span style='width:27px;  height:19px;  background:url(images/icon.png) repeat-y; background-position:0px 0px
'></span>";*/
	echo "1";
	}
}else{
	//echo "验证码不能为空!";
	echo "0";
}

?>

vcode_js.js
/*

GetXmlHttpObject():
	This function is checked for brower which is different.
	And choose right brower,to create XMLHttpRequest object.
*/
var xmlHttp;
var bl="0";
function GetXmlHttpObject()
{
	try{// Firefox,Opera 8.0+,Safari
	xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{// Internet Explorer
 		try{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}
 		catch (e){xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}
 	}
 	if(!xmlHttp){
 	alert("Cant Not Create XMLHttpRequest Object,Please Choose Other Brower");
 	}
	return xmlHttp;
}

function vcode_check(id){
	GetXmlHttpObject();
	var str=document.getElementById(id).value;
	var url="ajaxs/vcode_check.PHP?vocode="+str;
	xmlHttp.onreadystatechange=DealData;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function DealData(){
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 {
 	if(xmlHttp.responseText=="0"){
 	bl="0";
 	document.getElementById("vcode_tx").innerHTML="value=0";}
 	else if(xmlHttp.responseText=="1"){
 	bl="1";
 	document.getElementById("vcode_tx").innerHTML="<font color=red>worng</font>";}
 	else if(xmlHttp.responseText=="2"){
 	bl="2";
 	document.getElementById("vcode_tx").innerHTML="right";}
 }
}

function check(){
if(bl=="0"){alert("vocode is null");return false;}else{
if(bl=="1"){alert("vocode is worng!please try again");return false;}
else if(bl=="2"){;return true;}}
}
原文链接:https://www.f2er.com/ajax/166766.html

猜你在找的Ajax相关文章