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,0,0);
$te=imagecolorallocate($im,255,255,255);
imagestring($im,rand(1,3),rand(1,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"  οnkeyup="javascript:vcode_check('yzm')"/>
<img id=vcode_img src="ajaxs/vcode.php" alt="看不清?点击换一张" align="absmiddle" οnclick="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=" 登录 " οnclick="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;}}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值