可以把生成验证码的代码独立出来,命名为checkcode.asp:
<%
'**********************************************
'* NAME:checkcode.asp *
'* CODE:netops(www.netop.cc) *
'* USE:生成xbm格式的验证码 *
'* TIME:2005.7 *
'**********************************************
on error resume next
dim i
dim countdata
countdata="1234567890"
dim rou,chkcode,chklen
chkcode=""
chklen = 4
randomize
for i=1 to 4
rou = int(rnd*10)
chkcode = chkcode cstr(rou)
next
dim strDigits
strDigits = Array(_
"0","0x3c","0x66","0x66","0x66","0x66","0x66","0x66","0x66","0x66","0x3c",_
"1","0x30","0x38","0x30","0x30","0x30","0x30","0x30","0x30","0x30","0x30",_
"2","0x3c","0x66","0x60","0x60","0x30","0x18","0x0c","0x06","0x06","0x7e",_
"3","0x3c","0x66","0x60","0x60","0x38","0x60","0x60","0x60","0x66","0x3c",_
"4","0x30","0x30","0x38","0x38","0x34","0x34","0x32","0x7e","0x30","0x78",_
"5","0x7e","0x06","0x06","0x06","0x3e","0x60","0x60","0x60","0x66","0x3c",_
"6","0x38","0x0c","0x06","0x06","0x3e","0x66","0x66","0x66","0x66","0x3c",_
"7","0x7e","0x66","0x60","0x60","0x30","0x30","0x18","0x18","0x0c","0x0c",_
"8","0x3c","0x66","0x66","0x66","0x3c","0x66","0x66","0x66","0x66","0x3c",_
"9","0x3c","0x66","0x66","0x66","0x66","0x7c","0x60","0x60","0x30","0x1c")
dim iCharWidth,iCharHeight,theBit,theNum,iRow,k,theOffset
dim imageStr
imageStr = ""
iCharWidth = 8
iCharHeight= 10*1
Response.ContentType ="image/x-xbitmap"
Response.Expires =0
Response.Write "#define counter_width "&iCharWidth*chklen&chr(13) & chr(10)
Response.Write "#define counter_height "&iCharHeight&chr(13) & chr(10)
Response.Write "static unsigned char counter_bits[]={"
for iRow=0 to iCharHeight-1
for i=1 to chklen
theBit=mid(chkcode,i,1)
k=0
do while k<ubound(strDigits)
if strDigits(k) = theBit then exit do
k=k iCharHeight 1
loop
if k>=ubound(strDigits) then k=0
theOffset = k 1
imageStr = imageStr (strDigits(theOffset iRow))&","
next
next
imageStr = left(imageStr,(len(imageStr)-1))
Response.Write imageStr
Response.Write "};"
session("chkCode") = chkcode
%>
在post.asp中,定义表单时,相应的代码改为:
REM 子过程,定义表单
Sub myform()
Response.Write "<table width=500 border=1 cellspacing=0 cellpadding=5 align=center>"&_
"<form name=form1 method=post action=default.asp>"&_
"<tr height=30><td rowspan=2 width=100 align=center>验证码</td>"&_
"<td><input type=text name=chkcode size=10> "&_
"<img src=checkcode.asp width=32 height=10 border=0></td></tr>"&_
"<tr height=30><td>请填写文本框右侧的验证码</td></tr>"&_
"<tr height=30><td colspan=2 align=center>"&_
"<input type=submit name=Submit value=提交></td></tr>"&_
"</form></table>"
end Sub
以上就是最简单的xbm型的验证码。但是这种格式的验证码,windows xp sp2的用户无法看到,原因是xp sp2取消了对xbm格式文件的支持。不过,可以通过修改注册表解决这个问题:打开注册表,找到HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Security,用右键增加一个 dword 值,改名为 BlockXBM,其值为 00000000 。改好后,重新打开游览器就可以了。也可以用文件编辑器编辑一文件,内容如下:
REGEDIT4
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Security]
"BlockXBM"=dword:00000000
将该文件保存为扩展名为reg的注册表文件,然后双击该文件即可。
除了xbm格式的验证码,也可以利用Adodb.Stream对象,开发出更复杂的BMP型验证码,可以从http://www.netop.cc/downloads/bmpchkcode.rar得到相关程序。利用该程序,可以生成数字、字母混合,彩色,且有彩色背景的验证码。
对于租用空间的朋友来说,使用第三方组件受到很大限制。如果拥有自己的服务器,可以试一下利用ASP绘图组件shotgraph生成验证码,该验证码是GIF格式的,具有很广泛的通用性。该组件可从http://www.netop.cc/downloads/shotgraph.zip得到。压缩包里有详细的示例程序和说明文档。
总之,生成验证码的办法很多,现在有很多第三方组件可以帮助你生成各种样式的验证码,上网找找看吧。
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




