这套验证码与网上流传的验证多少有点不同。这里的图片是单张分开的,便于修改
writeCheckCode.asp 通过本文件实现写图片功能,可以定义图片所以路径、文件名等
<%
‘****—-参数设定—-****
imgFolderPath=”images/CheckCode/” ‘**存放路径
fileExt=”.gif” ‘**文件扩展名,要加上圆点的。可以使用的文件类型为gif jpeg bmp png
imgWidth=15 ‘**图像宽
imgHeight=20 ‘**图像高
dim imgFileName(10) ‘**文件名,不要加扩展名
imgFileName(0)=”0″
imgFileName(1)=”1″
imgFileName(2)=”2″
imgFileName(3)=”3″
imgFileName(4)=”4″
imgFileName(5)=”5″
imgFileName(6)=”6″
imgFileName(7)=”7″
imgFileName(8)=”8″
imgFileName(9)=”9″
‘****—-写图片—-****
no=request.querystring(“no”)
CheckCodeStr=session(“CheckCodeStr”)
url=imgFolderPath+imgFileName(cint(mid(CheckCodeStr,no,1)))+fileExt
select case lCase(fileExt)
case “.gif” response.ContentType=”image/gif”
case “.jpg” response.ContentType=”image/jpeg”
case “.bmp” response.ContentType=”image/bmp”
case “.png” response.ContentType=”image/png”
end select
set Ados=Server.CreateObject(“Adodb.Stream”)
Ados.Mode=3
Ados.Type=1
Ados.Open
Ados.LoadFromFile(server.mappath(url))
response.BinaryWrite Ados.read(Ados.size)
%>
CheckCodeFun.asp 显示验证码及检验是否正确
<%
action=request.querystring(“action”)
if action=”show” then
‘****—-生成验证码—-****
dim CheckCode(3)
randomize()
for i=0 to 3
CheckCode(i)=cint(rnd()*10-0.5)
CheckCodeStr=CheckCodeStr+cStr(CheckCode(i))
next
session(“CheckCodeStr”)=CheckCodeStr ‘**验证码存于session(“CheckCodeStr”)
str=”<body topmargin=0 leftmargin=0><div onclick=’document.location.reload()’ style=’cursor:hand’>”
for i=1 to 4
str=str+”<img alt='<–看不清,换一组验证码–>’ src=’writeCheckCode.asp?no=”+cstr(i)+”‘>”
next
‘str=str+” <input type=button value=’换一个’ onclick=’document.location.reload()’>” ‘如果不需要此按钮,可以把本行注释掉
str=str+”</div></body>”
response.write (str)
response.end
end if
if action=”check” then
CheckCode=request.querystring(“CheckCode”)
Response.charset = “gb2312”
if (CheckCode=session(“CheckCodeStr”)) then
response.write “yes”
else
response.write “<font color=red>验证码不正确</font>”
end if
end if
%>
AjaxFun.js Ajax调用函数
function createXMLHttpRequest()
{
try
{
xmlHttp = new XMLHttpRequest();
return xmlHttp;
}
catch(trymicrosoft)
{
try
{
xmlHttp=new ActiveXObject(“Msxml2.XMLHTTP”);
return xmlHttp;
}
catch(othermicrosoft)
{
try
{
xmlHttp = new ActiveXObject(“Microsoft.XMLHTTP”);
return xmlHttp;
}
catch(failed)
{
return xmlHttp;
}
}
}
if (!xmlHttp)
{
return false;
}
}
function getResponseText(method,url,obj)
{
xmlHttp=createXMLHttpRequest();
if (xmlHttp==null)
{
return “[error]”;
}
xmlHttp.open(method,url);
xmlHttp.onreadystatechange=function()
{
if(4==xmlHttp.readyState)
{
if(200==xmlHttp.status)
{
obj.innerHTML=xmlHttp.responseText;
xmlHttp=null;
}
}
}
xmlHttp.send();
}
test.htm 测试
<body>
<script language=javascript src=”AjaxFun.js”></script>
<script language=javascript>
function checkcode()
{
cc=CheckCodeText.value; //用户输入的验证码
objHint=document.getElementById(“CheckCodeHint”);
objHint.innerHTML=”正在验证….”;
getResponseText(‘get’,’CheckCodeFun.asp?action=check&CheckCode=’+cc,objHint);
}
</script>
<iframe id=”CheckCode” src=”CheckCodeFun.asp?action=show” name=”CheckCode” width=”75″ height=”16″ scrolling=”no” border=”0″ frameborder=”0″></iframe>
<input type=text name=”CheckCodeText”>
<input type=button value=”Check” onclick=”checkcode()”> <div id=”CheckCodeHint”></div>
</body>