ASP将数字相加求和生成BMP图片验证码
现在的广告推广是越来越嚣张了,时不时网站给发几个广告,所以我们可以在评论和留言的时候利用验证码来阻止一部分的广告。目前网络上常用的是图片验证码的形式,下面给出的是增强型验证码,即把数字求和采用asp程序生成Bmp图片格式,并且把数字相加后的和存进session里,这样就能达到很大程度上的防广告灌水效果。你也可以把它改进成诸如 3x87 23x9 等形式的。代码如下:
- <%
- Response.Buffer = True
- Response.ExpiresAbsolute = Now() - 1
- Response.Expires = 0
- Response.cachecontrol = "no-cache"
- Response.ContentType = "Image/Bmp"
- Call Com_CreatValidCode()
- Sub Com_CreatValidCode()
- Randomize
- Dim i, ii, iii
- Const cAmount = 10 '数值个数
- Const cCode = "0123456789" '数值范围
- Dim vColorData(2)
- vColorData(0) = "" '黑色点要转为彩色点,暂时不定义
- vColorData(1) = ChrB(255) & ChrB(255) & ChrB(255) '白点
- Dim vCode(4), vCodes '产生一组任意数
- For i = 0 To 3
- vCode(i) = Int(Rnd * cAmount) '数组等于总数*随机数
- if i=1 then vCode(i)="11" '第二位为+
- if i=3 then vCode(i)="10" '第四位为=
- vCodes=vCodes&Mid(cCode,vCode(i)+1,1) '串等于串加上后续值
- Next
- session("checkcode")=int(Mid(vCodes,1,1)) + int(Mid(vCodes,2,1)) '计算相加和值并赋值给session
- Dim vNumberData(36)
- vNumberData(0) =
- "1110000111110111101111011110111101111011110111101111011110111101111011110111101111011110111110000111"
- vNumberData(1) =
- "1111011111110001111111110111111111011111111101111111110111111111011111111101111111110111111100000111"
- vNumberData(2) =
- "1110000111110111101111011110111111111011111111011111111011111111011111111011111111011110111100000011"
- vNumberData(3) =
- "1110000111110111101111011110111111110111111100111111111101111111111011110111101111011110111110000111"
- vNumberData(4) =
- "1111101111111110111111110011111110101111110110111111011011111100000011111110111111111011111111000011"
- vNumberData(5) =
- "1100000011110111111111011111111101000111110011101111111110111111111011110111101111011110111110000111"
- vNumberData(6) =
- "1111000111111011101111011111111101111111110100011111001110111101111011110111101111011110111110000111"
- vNumberData(7) =
- "1100000011110111011111011101111111101111111110111111110111111111011111111101111111110111111111011111"
- vNumberData(8) =
- "1110000111110111101111011110111101111011111000011111101101111101111011110111101111011110111110000111"
- vNumberData(9) =
- "1110001111110111011111011110111101111011110111001111100010111111111011111111101111011101111110001111"
- '表示=
- vNumberData(10) =
- "1111111111111111111111111111111000000001111111111111111111111000000001111111111111111111111111111111"
- '表示+
- vNumberData(11) =
- "1111111111111100111111110011111111001111100000000110000000011111001111111100111111110011111111111111"
- Response.BinaryWrite ChrB(66) & ChrB(77) & ChrB(230) & ChrB(4) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) &_
- ChrB(0) & ChrB(0) & ChrB(54) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(40) & ChrB(0) &_
- ChrB(0) & ChrB(0) & ChrB(40) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(10) & ChrB(0) &_
- ChrB(0) & ChrB(0) & ChrB(1) & ChrB(0)
- Response.BinaryWrite&nb
sp;ChrB(24) &
ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(176) & ChrB(4) &_ - ChrB(0) & ChrB(0) & ChrB(18) & ChrB(11) & ChrB(0) & ChrB(0) & ChrB(18) & ChrB(11) &_
- ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) &_
- ChrB(0) & ChrB(0)
- For i = 9 To 0 Step -1 '行
- For ii = 0 To 3 '字数
- For iii = 1 To 10 '像素点
- if Mid(vNumberData(vCode(ii)), i * 10 + iii , 1) ="0" then '产生彩色点,减去一个固定值,可使颜色偏深
-
dim a,b,c
-
a=abs(Rnd * 256-60)
-
b=abs(Rnd * 256-128)
-
c=abs(Rnd * 256-60)
-
vColorData(0) = ChrB(a) & ChrB(b) & ChrB(c)
-
Response.BinaryWrite vColorData(Mid(vNumberData(vCode(ii)), i * 10 + iii , 1))
- else
-
dim d,e,f
'产生彩色背景,颜色偏浅,也可以随机生成噪点做背景 -
d=abs(Rnd * 255)
-
e=abs(Rnd * 255)
-
f=abs(Rnd * 255)
-
if d+e+f>640 then
'可改值,调整背景色深 -
vColorData(1) = ChrB(d) & ChrB(e) & ChrB(f)
-
Response.BinaryWrite vColorData(Mid(vNumberData(vCode(ii)), i * 10 + iii , 1))
-
else
-
Response.BinaryWrite vColorData(Mid(vNumberData(vCode(ii)), i * 10 + iii , 1))
-
end if
- end if
- Next
- Next
- Next
- End Sub
- %>
直接将以上代码另存为一个文件,如yzcode.asp,然后通过图片的方式调用此文件,如<img src="yzcode.asp" border=0 align="absmiddle" />,然后在提交后的处理页面里验证session("checkcode")的值就可以了。
最后,就是“5+6=”这样的随机数字相加的形式生成在bmp图片里了,它们的和需要你计算后填入,以达到验证的功能,防止机器蹂躏你的网站。
PS:大家在拷贝的时候,没有注意有些换行不是必要的;请认真对照代码