正 文:
前阵子学习ASPJPEG,发现用它生成随机验证码也是一个不错的方法。
建立2个文件:yz.asp和yzok.asp,同时准备一张底色透明的gif图片y1.gif 。
yz.asp代码:
<%
Response.Expires = 0
'生成安全码/验证码的函数
function make_randomize(max_len,w_n) 'max_len 生成长度,w_n:0包含字母,1:只为数字
randomize
for intcounter=1 to max_len
whatnext=int((1-0+1)*rnd+w_n)
if whatnext=0 then
upper=122
lower=97
else
upper=57
lower=48
end if
strnewpass=strnewpass & chr(int((upper-lower+1)*rnd)+lower)
next
make_randomize=strnewpass
end function
random_num=make_randomize(4,1) '生成4位数字的安全码
session("random_num")=random_num '调用session
Set Jpeg = Server.CreateObject("Persits.Jpeg")
Jpeg.Open Server.MapPath("y1.gif")
Jpeg.Canvas.Font.Color = &H006699
Jpeg.Canvas.Font.Family = "宋体"
Jpeg.Canvas.Font.Quality = 3
Jpeg.Canvas.Font.Bold = false
Jpeg.Canvas.PrintText 0, -2, random_num
jpeg.save Server.MapPath("y2.gif") '保存图片
set jpeg=nothing
response.write "<br><img src=y2.gif><br>"
response.write "<form name=""yz"" method=""post"" action=""yzok.asp""><input name=""y"" type=""text"" size=""10"" value=""""> <input type=""submit"" name=""Submit"" value=""Submit""></form>"
%>
然后到yzok.asp页验证:
<%
if session("random_num")<>request.form("y") then
response.write "验证码错误!"
else
response.write "验证码正确!"
end if
response.write "<a href=yz.asp>返回</a>"
%>