ASP-generated verification code detailed tutorial

Source: Internet
Author: User

There are a total of three files. Explains how to generate the verification code through ASP.
ASP File: code.asp
Data file: Body. Fix, Head.fix
Usage:

<%option explicitresponse.buffer=truenumcodefunction Numcode ()     Response.Expires = -1     Response.AddHeader "Pragma", "No-cache"     Response.AddHeader "Cache-ctrol", "No-cache"     Dim znum,i,j    Dim ados,ados1    Randomize timer    Znum = CInt (8999*rnd+1000)     session ("GetCode") = znum    Dim zimg (4),nstr         nstr=cstr (znum)         for i=0 to 3             zimg (i) =cint (Mid (nstr,i+1,1))          next    Dim pos    Set Ados=server.createobject ("ADODB.stream")         ados.mode=3        ados.type=1         ados.open    Set Ados1=server.createobject ("ADODB.stream")         ados1.mode=3         ados1.type=1        ados1.open         Ados.loadfromfile (Server.MapPath) ("body." Fix "))         Ados1.write ados.read (1280)          for i=0 to 3            ados.position= (9-zimg (i)) *320             ados1.position=i*320             ados1.write Ados.read (+)         next            Ados.loadfromfile (Server.mappath (" Head.fix "))         Pos=lenb (Ados.read ())          ados.position=pos        for I=0 To the 9 step 1            for J=0 to 3                 ados1.position=i*32+j*320                 ados.position=pos+30*j+i*120                 Ados.write Ados1.read (30)             next         next        Response.ContentType = "Image/bmp"          ados.position=0        response.binarywrite Ados.read ()         Ados.Close:set ados=nothing        Ados1.Close:set ados1=nothingend Function ' Asp code Created by blueidea.com Web team V37 2003-7-25%>

Code Explanation:

Response.buffer=true
Numcode
Function Numcode ()
response.expires =-1
response.addheader "Pragma", "No-cache"
response.addheader "Cache-ctrol", "No-cache"
dim Znum,i,j
dim ADOS,ADOS1
randomize Timer

Generate a random four-digit number:

Znum = CInt (8999*rnd+1000)

Pass to session

Session ("GetCode") = Znum

The For loop is an array of random numbers placed in a subscript 3 to facilitate subsequent array transformations

dim zimg (3), NSTR
nstr=cstr (Znum)
for i=0 to 3
zimg (i) =cint (Mid (nstr,i+1,1))
next
dim Pos

' Define two ADODB. Stream binary object for image data manipulation:

set ados=server.createobject ("ADODB.stream")
ados.mode=3
ados.type=1
ados.open
set ados1=server.createobject ("ADODB.stream")
ados1.mode=3
ados1.type=1
ados1.open

' Load 0~9 digital data 10x100, GBR array data, each 320 byte, 10 digits 3200byte
' BGR a point, 10x10 a number, a point three bytes (binary 8-bit, 16-system 00~FF)
' Line 10 points 30 bytes + Line end tag 00 2 bytes 32 bytes, so a 10x100 width is less than long image each digit 10x10 is 320 bytes
' The No Line end tag 0000, directly 300 bytes.
These are the BMP 24bit data details.
' As for the head, also very simple, contains the long width, the image starts the mark and so on ~ ~ Only 54 bytes, far does not have JPG what the complex

Ados.loadfromfile (Server.MapPath) ("body." Fix "))
Ados1.write Ados.read (1280)

' The first for loop, which extracts the corresponding four digits from the 10x100 digital array by the generated random number sequence
But vertical arrays of numbers

For I=0 to 3
ados.position= (9-zimg (i)) *320
ados1.position=i*320
ados1.write Ados.read (320)
Next

' Empty the ADOs data that has been used, and transfer the header file to replace the new image header 54 bytes 

Ados.loadfromfile (Server.MapPath ("Head.fix"))
Pos=lenb (Ados.read ())
Ados.position=pos

' Specify POS location, and then offset 54-byte locations to add graphic data
The second for loop, a numeric array transformation, and a vertical block converted to a horizontal number block
The method is to extract 4 times 30 bytes of the ADOs object from 320 bytes, and then extract the image data from the second row.
30 bytes is due to BMP wide long no 00 00 Line end Tag

The following are the referenced contents:
For i=0 to 9 Step 1
for j=0 to 3
ados1.position=i*32+j*320
ados.position=pos+30*j+i*120
ados.write Ados1.read (30)
next
Next
Ados.position=0
Response. BinaryWrite send image data directly to the client
Response.ContentType = "Image/bmp"
Response.BinaryWrite Ados.read ()
Ados.Close:set ados=nothing
Ados1.Close:set ados1=nothing
End Function



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.