网页的验证码如何制作?
发布网友
发布时间:2022-04-22 09:21
我来回答
共1个回答
热心网友
时间:2023-10-25 07:19
分类: 电脑/网络 >> 互联网
问题描述:
哪位告诉我在网站做验证怎么做啊啊谢谢
解析:
还是从最简单的例子说起,来了解一下验证码的基本思路。
第一个例子,在显示表单的同时,生成一个4位的随机数做为验证码,利用session传递该验证码,在数据处理页面,比对用户输入的验证码与session中的值是否一致。顺便说一句,我曾见过有糊涂的仁兄利用hidden类型的input控件传递验证码,孰不知,通过查看页面源代码,该数据是暴露无遗的。还有password型的input中的数据也一样。
<%
''''''''**********************************
''''''''* NAME:post *
''''''''* CODE:ops(op.cc) *
''''''''* USE:验证码示例一:数字型 *
''''''''* TIME:2005.7 *
''''''''**********************************
Response.Buffer = true
Dim CheckCode
Response.Write "<><body>"
''''''''======***表单提交后:***======
if Request.ServerVariables("REQUEST_METHOD")="POST" then
Dim founderr,callform,msg
founderr = false ''''''''是否有错误发生
callform = false ''''''''是否调用表单
msg = "" ''''''''提示信息
''''''''==============验证码验证开始===========
dim sessionCode
sessionCode = session("cCode")
session("cCode") = ""
CheckCode = trim(Request.Form("ccode"))
if CheckCode = "" then
msg = msg + "<li>请填写验证码"
founderr = true
callform = true
elseif cstr(CheckCode) <> cstr(sessionCode) then
msg = msg + "<li>验证码不正确"
founderr = true
callform = true
end if
''''''''==================验证码验证结束============
if founderr = true then
call message("500")
if callform = true then call myform()
else
msg = "<li>操作成功!"
call message("500")
end if
''''''''======***页面初始化(表单提交前)***======
else
CheckCode = ""
call myform()
end if
Response.Write "</body></>"
REM 子过程,定义表单
Sub myform()
Response.Write "<table width=500 border=1 cellspacing=0 cellpadding=5 align=center>"&_
"<form name=form1 method=post action=post>"&_
"<tr height=30><td rowspan=2 width=100 align=center>验证码</td>"&_
"<td><input type=text name=ccode size=10> "&_
getCCode()&"</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
Rem 子函数,生成验证码(四位随机数)
function getCCode()
dim ranNum
randomize
ranNum=int(9000*rnd)+1000
session("cCode") = ranNum
getCCode = ranNum
end function
Rem 提示信息
sub message(w)
Response.Write "<table width=&w& border=1 cellspacing=0 cellpadding=5 align=center>"&_
"<tr height=30><td>提示信息</td></tr>"&_
"<tr valign=top><td class="page_speeder_327763530"color:red;"">"&msg&"</td></tr></table>"
end sub
%>
以上代码展示生成验证码的一般思路,生成一个四位随机数作为验证码,这是最简单,同时也是最不安全的一种方法。
或许你想到了,可以以每位数字对应一张图片,生成图片型的验证码,就象图片型计数器那样处理。这并不是一个好想法,它跟数值型的验证码没有本质上的区别,并不能提高安全性。
下面,介绍如何生成图片型的验证码。
首先,可以通过ASP生成xbm型的验证码,这的的确确是一张xbm格式的图片,而且,你可以任意设置图片的大小。为了简单起见,我们仍以数字为例。
可以把生成验证码的代码独立出来,命名为checkcode:
<%
''''''''**********************************************
''''''''* NAME:checkcode *
''''''''* CODE:ops(op.cc) *
''''''''* USE:生成xbm格式的验证码 *
''''''''* TIME:2005.7 *
''''''''**********************************************
on error resume next
dim i
dim countdata
countdata="***********"
dim rou,ccode,clen
ccode=""
clen = 4
randomize
for i=1 to 4
rou = int(rnd*10)
ccode = ccode + 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*clen&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 clen
theBit=mid(ccode,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("cCode") = ccode
%>
在post中,定义表单时,相应的代码改为:
REM 子过程,定义表单
Sub myform()
Response.Write "<table width=500 border=1 cellspacing=0 cellpadding=5 align=center>"&_
"<form name=form1 method=post action=default>"&_
"<tr height=30><td rowspan=2 width=100 align=center>验证码</td>"&_
"<td><input type=text name=ccode size=10> "&_
"<img src=checkcode 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\Inter Explorer\Security,用右键增加一个 dword 值,改名为 BlockXBM,其值为 *********** 。改好后,重新打开游览器就可以了。也可以用文件编辑器编辑一文件,内容如下:
REGEDIT4
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Inter Explorer\Security]
"BlockXBM"=dword:***********
将该文件保存为扩展名为reg的注册表文件,然后双击该文件即可。
除了xbm格式的验证码,也可以利用Adodb.Stream对象,开发出更复杂的BMP型验证码,可以从op.cc/downloads/bmpccode.rar得到相关程序。利用该程序,可以生成数字、字母混合,彩色,且有彩色背景的验证码。
对于租用空间的朋友来说,使用第三方组件受到很大*。如果拥有自己的服务器,可以试一下利用ASP绘图组件shotgraph生成验证码,该验证码是GIF格式的,具有很广泛的通用性。该组件可从op.cc/downloads/shotgraph.zip得到。压缩包里有详细的示例程序和说明文档。
总之,生成验证码的办法很多,现在有很多第三方组件可以帮助你生成各种样式的验证码,上网找找看吧。