服务端 jscript 生成随机数图片验证码 by shawl.qiu
说明:其实这个程序只是从某人写的一个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 源代码
- <%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>shawl.qiu template</title>
- </head>
- <body>
- <%
- var sCk=Request.QueryString('id')+'';
- switch(sCk){
- case 'check':
- Response.write("Session('checkcode'): "+Session('checkcode'));
- Response.write("<br/>Request.Form('checkcode'): "+Request.Form('checkcode'));
- break;
- default:
- %>
- <img src='checkcode.asp' />
- <form action='?id=check' method='post'>
- the code:<input type='text' size='4' name='checkcode'/>
- <input type='submit' /> <input type='reset' />
- </form>
- <%
- }
- %>
- </body>
- </html>
- <%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%>
- <%
- checkcode(1000,9999,'checkcode');
- function checkcode(nStart, nOver, sSession){
- if(!nStart) nStart=1000;
- if(!nOver) nOver=9999;
- if(!sSession) sSession='checkcode';
- var ar=[10];
- //0
- ar[0]=new Array('0x3c', '0x66', '0xc3', '0xc3', '0xc3', '0xc3', '0xc3', '0xc3', '0x66', '0x3c');
- //1
- ar[1]=new Array('0x18', '0x1c', '0x18', '0x18', '0x18', '0x18', '0x18', '0x18', '0x18', '0x7e');
- //2
- ar[2]=new Array('0x3c', '0x66', '0x60', '0x60', '0x30', '0x18', '0x0c', '0x06', '0x06', '0x7e');
- //3
- ar[3]=new Array('0x3c', '0x66', '0xc0', '0x60', '0x1c', '0x60', '0xc0', '0xc0', '0x66', '0x38');
- //4
- ar[4]=new Array('0x38', '0x3c', '0x36', '0x33', '0x33', '0x33', '0xff', '0x30', '0x30', '0xfe');
- //5
- ar[5]=new Array('0xfe', '0xfe', '0x06', '0x06', '0x3e', '0x60', '0xc0', '0xc3', '0x66', '0x3c');
- //6
- ar[6]=new Array('0x60', '0x30', '0x18', '0x0c', '0x3e', '0x63', '0xc3', '0xc3', '0x66', '0x3c');
- //7
- ar[7]=new Array('0xff', '0xc0', '0x60', '0x30', '0x18', '0x18', '0x18', '0x18', '0x18', '0x18');
- //8
- ar[8]=new Array('0x3c', '0x66', '0xc3', '0x66', '0x3c', '0x66', '0xc3', '0xc3', '0x66', '0x3c');
- //9
- ar[9]=new Array('0x3c', '0x66', '0xc3', '0xc3', '0x66', '0x3c', '0x18', '0x0c', '0x06', '0x03');
- //Response.Buffer = false;
- with(Response){
- Expires = -1 ;
- AddHeader("Pragma","no-cache") ;
- AddHeader("cache-ctrol","no-cache");
- }
- var nNum=fRandomBy(nStart,nOver)+'';
- Session(sSession)=nNum;
- //Response.write(Session(sSession));
- var arDg=[];
- for(var i=0; i<nNum.length; i++){
- arDg[i]=nNum.charAt(i);
- }
- var sMarker='/r/n';
- var nWidth=8*arDg.length;
- var nHeight=10;
- Response.ContentType = "image/x-xbitmap"
- var sImg='#define counter_width '+nWidth+sMarker+
- '#define counter_height '+nHeight+sMarker+
- 'static unsigned char counter_bits[] = {'+sMarker
- for(var i=0; i<10; i++){
- for(var j=0, k=arDg.length; j<k; j++){
- sImg+=ar[arDg[j]][i]+',';
- }
- }
- sImg=sImg.replace(//,$/,'');
- sImg+='};'+sMarker;
- Response.write(sImg);
- function fRandomBy(nUnder, nOver){
- switch(arguments.length){
- case 1: return parseInt(Math.random()*nUnder+1);
- case 2: return parseInt(Math.random()*(nOver-nUnder+1) + nUnder);
- default: return 0;
- }
- } // shawl.qiu code
- } // end function checkcode(nStart, nOver, sSession) // shawl.qiu code
- %>