为了指导学生竞赛,为了让初学者快速学会验证码的使用方法,编写了一个简单的实现原理:
testcode.asp 显示验证码页面
==============
<form id="form1" name="form1" method="post" action="checkcode.asp">
<input name="textfield" type="text" id="textfield" size="5" />
<%
Randomize ‘生成随机种子
an= int(100000*rnd()) ‘rnd是随机函数是个小数,前面的100000是为了生成多个位数
session("code")=an '存在session里供以后验证用
Response.Write an '显示验证码
%>
<input type="submit" name="button" id="button" value="提交" />
</form>
checkcode.asp 验证验证码正确与否
==============
<%
if request.Form("textfield")="" or trim(session("code"))<>trim(request.Form("textfield")) then ‘有两种可能,一种是没有输入值,一种是输入值错误
response.Write("验证码不正确,请重新输入")
response.end()
end if
response.Write("输入正确")
%>
这个验证方法,其实只能实现一个简单的验证码功能,因为产生的数字是可以复制的,所以在某种程度上并不能绕过机器程序的捕捉。更好的方法是用xbm或随机生成图象来完成,但xbm有些sp2机器上不识别,随机生成图象的方法太复杂。所以先用这个教学测试用吧。