网站地图 | RSS订阅 凌陈亮博客 - 专业上海SEO上海SEO优化,分享网站优化知识,同时提供上海SEO服务。
你的位置:首页 » ASP程序 » 正文

ASP生成数字字母图片验证码,可设置位数

2015-12-31 10:0:13 | 作者:凌陈亮 | 3个评论 | 人浏览

点击此处在线预览效果

HTML中调用代码如下:

<img src="getcheckcode.asp?checkcodename=checkcode" alt="验证码" border="0" width="60" height="20" title="看不清?点一下" align="bottom" onclick="javascript:this.src='getcheckcode.asp?checkcodename=checkcode&getrandom=' + Math.random() ;" style="cursor:pointer;" />

 

ASP文件名称为:getcheckcode.asp,代码如下(仅此一个ASP文件即可生成,无需其它文件):

<%

'制作:凌陈亮www.lingchenliang.com(QQ:57404811)
'此为修改网上下的代码,现可设置验证码位数,字符点阵二进制码(独创12*12点阵)和部分程序已改,另增加注释说明

Call Com_CreatValidCode(trim(request.QueryString("checkcodename")))

Sub Com_CreatValidCode(pSN) '参数:验证码SESSION名称

    Response.Expires = -9999
    Response.AddHeader "Pragma","no-cache"
    Response.AddHeader "cache-ctrol","no-cache"
    Response.ContentType = "Image/BMP"

    Randomize

    Dim i, ii, iii
 
 const cnum=4 '验证码位数,从0开始,3表示4位
 
    Const cOdds = 0 ' 杂点出现的机率
    'Const cAmount = 10 ' 文字数量:前面多少个字符中随机筛选,10为纯数字
    Const cAmount = 36 ' 文字数量,36为数字字母混合
    Const cCode = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
   const cwidth=12 '字宽(要为4的倍数),固定值,不可修改
   const cheight=12 '字高,固定值,不可修改

    ' 随机产生字符
    redim vCode(cnum)
 dim vCodes
    For i = 0 To cnum
        vCode(i) = Int(Rnd * cAmount)
        vCodes = vCodes & Mid(cCode, vCode(i) + 1, 1)
    Next

    Session(pSN) = vCodes '记录入Session

    ' 字符的数据
    Dim vNumberData(35)
    vNumberData(0) = "111111111111111000000111110111111011110111111011110110011011110110011011110110011011110110011011110111111011110111111011111000000111111111111111"
    vNumberData(1) = "111111111111111110111111111000111111111110111111111110111111111110111111111110111111111110111111111110111111111110111111111000001111111111111111"
    vNumberData(2) = "111111111111111100001111111011110111111011110111111111110111111111101111111111011111111110111111111101111111111011110111111000000111111111111111"
    vNumberData(3) = "111111111111111100001111111011110111111011110111111111101111111110011111111111101111111111110111111011110111111011110111111100001111111111111111"
    vNumberData(4) = "111111111111111111011111111111011111111110011111111101011111111011011111111011011111111000000111111111011111111111011111111110000111111111111111"
    vNumberData(5) = "111111111111111000000111111011111111111011111111111010001111111001110111111111110111111111110111111011110111111011110111111100001111111111111111"
    vNumberData(6) = "111111111111111110001111111101110111111011111111111011111111111010001111111001110111111011110111111011110111111011110111111100001111111111111111"
    vNumberData(7) = "111111111111111000000111111011101111111011101111111111011111111111011111111110111111111110111111111110111111111110111111111110111111111111111111"
    vNumberData(8) = "111111111111111100001111111011110111111011110111111011110111111100001111111101101111111011110111111011110111111011110111111100001111111111111111"
    vNumberData(9) = "111111111111111100011111111011101111111011110111111011110111111011100111111100010111111111110111111111110111111011101111111100011111111111111111"
    vNumberData(10) = "111111111111111110111111111110111111111101011111111101011111111101011111111101011111111000001111111011101111111011101111110001000111111111111111"
    vNumberData(11) = "111111111111110000001111111011110111111011110111111011101111111000011111111011101111111011110111111011110111111011110111110000001111111111111111"
    vNumberData(12) = "111111111111111100000111111011110111110111110111110111111111110111111111110111111111110111111111110111110111111011101111111100011111111111111111"
    vNumberData(13) = "111111111111110000011111111011101111111011110111111011110111111011110111111011110111111011110111111011110111111011101111110000011111111111111111"
    vNumberData(14) = "111111111111110000001111111011110111111011011111111011011111111000011111111011011111111011011111111011111111111011110111110000001111111111111111"
    vNumberData(15) = "111111111111110000001111111011110111111011011111111011011111111000011111111011011111111011011111111011111111111011111111110001111111111111111111"
    vNumberData(16) = "111111111111111100001111111011101111110111101111110111111111110111111111110111111111110111000111110111101111111011101111111100011111111111111111"
    vNumberData(17) = "111111111111110001000111111011101111111011101111111011101111111000001111111011101111111011101111111011101111111011101111110001000111111111111111"
    vNumberData(18) = "111111111111111000001111111110111111111110111111111110111111111110111111111110111111111110111111111110111111111110111111111000001111111111111111"
    vNumberData(19) = "111111111111111100000111111111011111111111011111111111011111111111011111111111011111111111011111111111011111110111011111110000111111111111111111"
    vNumberData(20) = "111111111111110001000111111011101111111011011111111010111111111000111111111010111111111011011111111011011111111011101111110001000111111111111111"
    vNumberData(21) = "111111111111110001111111111011111111111011111111111011111111111011111111111011111111111011111111111011111111111011110111110000000111111111111111"
    vNumberData(22) = "111111111111110001000111111001001111111001001111111001001111111010101111111010101111111010101111111010101111111010101111110010100111111111111111"
    vNumberData(23) = "111111111111110001000111111001101111111001101111111010101111111010101111111010101111111011001111111011001111111011001111110001101111111111111111"
    vNumberData(24) = "111111111111111100011111111011101111110111110111110111110111110111110111110111110111110111110111110111110111111011101111111100011111111111111111"
    vNumberData(25) = "111111111111110000001111111011110111111011110111111011110111111000001111111011111111111011111111111011111111111011111111110001111111111111111111"
    vNumberData(26) = "111111111111111100011111111011101111110111110111110111110111110111110111110111110111110111110111110100110111111011001111111100010111111111111111"
    vNumberData(27) = "111111111111110000011111111011101111111011101111111011101111111000011111111010111111111011011111111011011111111011101111110001100111111111111111"
    vNumberData(28) = "111111111111111100000111111011110111111011110111111011111111111100111111111111001111111111110111111011110111111011110111111000001111111111111111"
    vNumberData(29) = "111111111111110000000111110110110111111110111111111110111111111110111111111110111111111110111111111110111111111110111111111100011111111111111111"
    vNumberData(30) = "111111111111110001000111111011101111111011101111111011101111111011101111111011101111111011101111111011101111111011101111111100011111111111111111"
    vNumberData(31) = "111111111111110001000111111011101111111011101111111011101111111101011111111101011111111101011111111101011111111110111111111110111111111111111111"
    vNumberData(32) = "111111111111110010100111111010101111111010101111111010101111111010101111111001001111111101011111111101011111111101011111111101011111111111111111"
    vNumberData(33) = "111111111111110001000111111011101111111101011111111101011111111110111111111110111111111101011111111101011111111011101111110001000111111111111111"
    vNumberData(34) = "111111111111110001000111111011101111111011101111111101011111111101011111111110111111111110111111111110111111111110111111111100011111111111111111"
    vNumberData(35) = "111111111111111000000111111011101111111111101111111111011111111111011111111110111111111110111111111101111111111101110111111000000111111111111111"
 dim picwidth : picwidth=(cnum+1)*cwidth '字符个数*字宽=图片宽度
 dim picdatasize : picdatasize=picwidth*cheight*3 '宽*高*3
 dim filesize : filesize=picdatasize+54
    ' 输出图像文件头
    Response.BinaryWrite ChrB(66) & ChrB(77) & ChrB(filesize mod 256) & chrB((filesize\256) mod 256) & ChrB(((filesize\256)\256) mod 256) & ChrB(((filesize\256)\256)\256) &_
  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(picwidth mod 256) & chrB((picwidth\256) mod 256) & ChrB(((picwidth\256)\256) mod 256) & ChrB(((picwidth\256)\256)\256) & ChrB(cheight mod 256) & chrB((cheight\256) mod 256) & ChrB(((cheight\256)\256) mod 256) & ChrB(((cheight\256)\256)\256) & ChrB(1) & ChrB(0)
'文件标识(2bytes):ChrB(66) & ChrB(77) 即BM
'文件大小(1dword):ChrB(230) & ChrB(4) & ChrB(0) & ChrB(0)
'保留字(1dword):ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0)           说明:保留,必须设置为0
'偏移量(1dword):ChrB(54) & ChrB(0) & ChrB(0) & ChrB(0)          说明:从文件开始到位图数据开始之间的数据偏移量
'位图信息头长度(1dword):ChrB(40) & ChrB(0) & ChrB(0) & ChrB(0) 
'位图的宽度(1word):ChrB(40) & ChrB(0) & ChrB(0) & ChrB(0)       说明:单位为像素
'位图的高度(1dword):ChrB(10) & ChrB(0) & ChrB(0) & ChrB(0)
'位图的位面数(1dword):ChrB(1) & ChrB(0)                         说明:该值总为1

    ' 输出图像信息头
    Response.BinaryWrite ChrB(24) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) &_
  ChrB(picdatasize Mod 256) & ChrB((picdatasize\256) Mod 256) & ChrB((picdatasize\256\256) Mod 256) & ChrB(picdatasize\256\256\256) & 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)
'每个像素的位数(2bytes):ChrB(24) & ChrB(0) 说明:1-单色位图;4-16色位图;8-256色位图;16-16bit;24-24bits;32-32bit
'压缩说明(1dword):ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) 说明:0-不压缩;1-rel8压缩;2-rel4压缩;3-bitfields一位域存放
'用字节数表示位图数据大小(1dword):ChrB(176) & ChrB(4) & ChrB(0) & ChrB(0) 说明:4的倍数;
'水平分辨率(1dword):ChrB(18) & ChrB(11) & ChrB(0) & ChrB(0)  说明:单位为,像素/米
'垂直分辨率(1dword):ChrB(18) & ChrB(11) & ChrB(0) & ChrB(0)
'位图使用的颜色数(1dword):ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0)
'重要颜色数(1dword):ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0)    说明:0-表示所有数据一样重要

 redim temp_color(cnum),tem_i(cnum)
    For i = cheight-1 To 0 Step -1 ' 历经所有行(按高的每1px逐行输出)
        For ii = 0 To cnum ' 历经所有字符
            For iii = 1 To cwidth ' 历经所有像素(按字符宽度逐像素输出)
            ' 逐行、逐字、逐像素地输出图像数据
                If Rnd * 99 + 1 < cOdds Then '随机生成杂点
     Response.BinaryWrite com_vColorData(1) '输出杂点
                Else
     dim abc : abc=clng(Mid(vNumberData(vCode(ii)), i * cwidth + iii, 1))
     if abc=1 then
      Response.BinaryWrite com_vColorData(0) '输出背景
     else
      if tem_i(ii)<>1 then
       tem_i(ii)=1
       temp_color(ii)=com_vColorData(1) '获取字符颜色
      end if
      Response.BinaryWrite temp_color(ii) '输出字符
     end if
                End If
            Next
        Next
    Next
End Sub

' 颜色的数据(背景/字符)
function com_vColorData(n)
 dim return
 if n=0 then '背景
  return=ChrB(250) & ChrB(240) & ChrB(230) '淡蓝色
 else '字符
  return=ChrB(clng(rnd*220)) & ChrB(clng(rnd*210)) & ChrB(clng(rnd*200)) '前景
 end if
 com_vColorData=return
end function

%>

 

  • 本文来自: 凌陈亮博客,转载请保留出处!欢迎发表您的评论
  • 相关标签:获取字符串  验证码  随机数  
  • 已有3位网友发表了一针见血的评论,你还等什么?

    必填

    选填

    记住我,下次回复时不用重新输入个人信息

    必填,不填不让过哦,嘻嘻。

    ◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。