When you log in online, you will often see a verification code that asks you to enter, some of which are text, and some of which are pictures. For example, when you leave a message in the chinaren.com alumni directory, we will see a digital picture verification code; about online There is a lot of information on how to implement digital text verification codes, and what we introduce here is how to implement verification codes that are randomly composed of numbers and letters and generate pictures. It looks complicated, but is actually very simple. Follow me and read on:
First, let's introduce the design idea. A random combination of numbers and letters generates a verification code, and then generates a picture of the verification code. The "combination of numbers and letters" here should be randomly taken out; if it is a special digital verification code, we This can be achieved like this:
ycodenum=4 'The number of digits in the verification code, or a number
for i=1 to ycodenum
Randomize 'Initialize the random number generator
ycode=ycode&Int((9*Rnd)) 'rnd is a random number, any real number between 0 and 1, here you get an integer between 0 and 9
next
response.write ycode 'will output the digital verification code (4 digits)
However, we want numbers and letters to be equally randomly generated. Here we can use an array to achieve this effect, as follows:
ychar="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N ,O,P,Q,R,S,T,U,V,W,X,Y,Z" 'Combine numbers and uppercase letters into a string
yc=split(char,",") 'Generate string into array
ycodenum=4
for i=1 to ycodenum
Randomize
ycode=ycode&yc(Int((35*Rnd))) 'Arrays generally start reading from 0, so here is 35*Rnd
next
response.write ycode
Now let’s see if the output is a random combination of numbers and letters?
Let's take a look at how to generate pictures. Some friends may know this: ASP cannot generate pictures, and ASP components must be used. Yes, we are using the ASP image component shotgraph here. One thing everyone should pay attention to is that you cannot use the server if it is not your own, because you cannot install this component.
Download address of the component: yc=split(char,",") 'Generate string into array
ycodenum=4
for i=1 to ycodenum
Randomize
ycode=ycode&yc(Int((35*Rnd))) 'Arrays generally start reading from 0, so here is 35*Rnd
next
Response.Clear
Response.ContentType="image/gif"
set obj=Server.CreateObject("shotgraph.image")
x=55 'The width of the picture
y=26 'Height of the picture
obj.CreateImage x,y,8 '8 is the color of the picture in 8 bits
obj.SetColor 0,55,126,222
obj.SetColor 1,255,255,255
obj.CreatePen "PS_SOLID",1,0
obj.SetBgColor 0
obj.Rectangle 0,0,x-1,y-1
obj.SetBkMode "TRANSPARENT"
obj.CreateFont "Arial",136,18,1,False,False,False,False
obj.SetTextColor 1
obj.TextOut 5,4,ycode&" "
img=obj.GifImage(-1,1,"")
Response.BinaryWrite(img)
For the above code, that is to say, the principle of ordinary drawing of shotgraph, please refer to: http://www.pconline.com.cn/pcedu/empolder/wz/asp/10204/45207.html