asfman
android developer
posts - 90,  comments - 213,  trackbacks - 0
http://blog.csdn.net/btbtd/archive/2007/01/04/1473710.aspx
说明:
 其实这个程序只是从某人写的一个VBScript程序改写成 Jscript 而已, 可以说不是我写的, 只是改改而已...
 至于VBScript版原作者是谁, 我不知道(现在的网络转载大都没署原作来历)...
 
 原理嘛, 就是预存0-9的十六进制数据在数组A里,
 然后生成一个随机数,
 把随机数存放进 Session 里, 以备进行提交检验,
 再生成一个数组B, 数组B内容的每个项对应随机数的一个字符.
 然后循环输出数组A, 以数组B的每一个项内容访问存放十六进制数组A的数据,
 最后使用 xbm 格式输出图片数据.
 
 鄙人弄了一个演示, 共两个文件.
 test.asp 调用生成随机图片数据.
 checkcode.asp 生成随机图片数据.
 
 下载原格式:
  http://files.myopera.com/btbtd/jscript_ss/function/jscript_rnd_num_pic_validator.7z

 目录:
 1. test.asp 源代码
 2. checkcode.asp 源代码
  
 shawl.qiu
 2007-01-04
 http://blog.csdn.net/btbtd
 
 1. test.asp 源代码
  1. <%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Typecontent="text/html; charset=utf-8" />
  6. <title>shawl.qiu template</title>
  7. </head>
  8. <body>
  9. <%
  10.  var sCk=Request.QueryString('id')+'';
  11.  switch(sCk){
  12.  case 'check':
  13.   Response.write("Session('checkcode'): "+Session('checkcode'));
  14.   Response.write("<br/>Request.Form('checkcode'): "+Request.Form('checkcode'));
  15.   break;
  16.  default:
  17. %>
  18. <img src='checkcode.asp' />
  19. <form action='?id=check' method='post'>
  20.  the code:<input type='textsize='4name='checkcode'/>
  21.  <input type='submit' /> <input type='reset' />
  22. </form>
  23. <%
  24.  }
  25. %>
  26. </body>
  27. </html>
 2. checkcode.asp 源代码
  1. <%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%>
  2. <%
  3.  checkcode(1000,9999,'checkcode');
  4.   function checkcode(nStart, nOver, sSession){
  5.   if(!nStart) nStart=1000;
  6.   if(!nOver) nOver=9999;
  7.   if(!sSession) sSession='checkcode';
  8.      var ar=[10];
  9.    //0
  10.    ar[0]=new Array('0x3c', '0x66', '0xc3', '0xc3', '0xc3', '0xc3', '0xc3', '0xc3', '0x66', '0x3c');
  11.    //1
  12.    ar[1]=new Array('0x18', '0x1c', '0x18', '0x18', '0x18', '0x18', '0x18', '0x18', '0x18', '0x7e');
  13.    //2
  14.    ar[2]=new Array('0x3c', '0x66', '0x60', '0x60', '0x30', '0x18', '0x0c', '0x06', '0x06', '0x7e');
  15.    //3
  16.    ar[3]=new Array('0x3c', '0x66', '0xc0', '0x60', '0x1c', '0x60', '0xc0', '0xc0', '0x66', '0x38');
  17.    //4
  18.    ar[4]=new Array('0x38', '0x3c', '0x36', '0x33', '0x33', '0x33', '0xff', '0x30', '0x30', '0xfe');
  19.    //5
  20.    ar[5]=new Array('0xfe', '0xfe', '0x06', '0x06', '0x3e', '0x60', '0xc0', '0xc3', '0x66', '0x3c');
  21.    //6
  22.    ar[6]=new Array('0x60', '0x30', '0x18', '0x0c', '0x3e', '0x63', '0xc3', '0xc3', '0x66', '0x3c');
  23.    //7
  24.    ar[7]=new Array('0xff', '0xc0', '0x60', '0x30', '0x18', '0x18', '0x18', '0x18', '0x18', '0x18');
  25.    //8
  26.    ar[8]=new Array('0x3c', '0x66', '0xc3', '0x66', '0x3c', '0x66', '0xc3', '0xc3', '0x66', '0x3c');
  27.    //9
  28.    ar[9]=new Array('0x3c', '0x66', '0xc3', '0xc3', '0x66', '0x3c', '0x18', '0x0c', '0x06', '0x03');
  29.     //Response.Buffer = false;
  30.     with(Response){
  31.    Expires = -1 ;
  32.    AddHeader("Pragma","no-cache") ;
  33.    AddHeader("cache-ctrol","no-cache"); 
  34.   }
  35.     var nNum=fRandomBy(nStart,nOver)+'';
  36.   Session(sSession)=nNum;
  37.     //Response.write(Session(sSession));
  38.     var arDg=[];
  39.   for(var i=0; i<nNum.length; i++){
  40.    arDg[i]=nNum.charAt(i);
  41.   }
  42.    var sMarker='\r\n';
  43.    var nWidth=8*arDg.length;
  44.   var nHeight=10; 
  45.     Response.ContentType = "image/x-xbitmap"
  46.     var sImg='#define counter_width '+nWidth+sMarker+
  47.     '#define counter_height '+nHeight+sMarker+
  48.     'static unsigned char counter_bits[] = {'+sMarker
  49.     for(var i=0; i<10; i++){
  50.      for(var j=0, k=arDg.length; j<k; j++){
  51.       sImg+=ar[arDg[j]][i]+',';
  52.      }
  53.     }
  54.      sImg=sImg.replace(/\,$/,'');
  55.     sImg+='};'+sMarker;
  56.   Response.write(sImg)
  57.     function fRandomBy(nUnder, nOver){
  58.    switch(arguments.length){
  59.     case 1: return parseInt(Math.random()*nUnder+1);
  60.     case 2: return parseInt(Math.random()*(nOver-nUnder+1) + nUnder);
  61.     defaultreturn 0;
  62.    }
  63.   }  // shawl.qiu code
  64.  } // end function checkcode(nStart, nOver, sSession) 
  65. %>
posted on 2007-11-04 23:21 汪杰 阅读(862) 评论(4)  编辑 收藏 引用 所属分类: asp(javascrpt)

FeedBack:
# re: 服务端 jscript 生成随机数图片验证码 by shawl.qiu
2008-12-25 17:41 | dfgdf
gdfgdfg  回复  更多评论
  
只有注册用户登录后才能发表评论。

<2006年7月>
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

常用链接

留言簿(15)

随笔分类(1)

随笔档案(90)

文章分类(727)

文章档案(712)

相册

收藏夹

http://blog.csdn.net/prodigynonsense

友情链接

最新随笔

搜索

  •  

积分与排名

  • 积分 - 457967
  • 排名 - 6

最新随笔

最新评论

阅读排行榜

评论排行榜