A verification code is an image that uses letters, numbers, and even Chinese characters as the content of an image. In this way, the content of an image can be easily identified by human eyes and cannot be identified by programs. Before database operations (such as logon verification, voting, posting, reply, registration, etc.), the program first verifies whether the Verification Code submitted by the client is the same as the content in the image. If the verification code is the same, the database operation is performed, if the verification code is different, the system prompts that the verification code is incorrect and no database operation is performed. In this way, various robot programs will be rejected!
The content of the verification code must be stored on the server. Generally, we can add the randomly generated verification code to the session. When the user submits the verification code, the submitted content is compared with the verification code in the session. The background code on the page where the verification code is generated can be written as follows:
Protected void page_load (Object sender, eventargs E)
{
String checkcode = createrandomcode (6 );
Session ["checkcode"] = checkcode;
Createimage (checkcode );
}
// Generate the verification code of the codecount bit
Private string createrandomcode (INT codecount)
{
String allchar = ", A, B, C, D, E, F, G, H, I, J, K, L, M, n, O, P, Q, R, S, T, U, w, x, y, z ";
String [] allchararray = allchar. Split (',');
String randomcode = "";
Int temp =-1;
Random Rand = new random ();
For (INT I = 0; I <codecount; I ++)
{
If (temp! =-1)
{
Rand = new random (I * temp * (INT) datetime. Now. ticks ));
}
Int T = Rand. Next (35 );
If (temp = T)
{
Return createrandomcode (codecount );
}
Temp = T;
Randomcode + = allchararray [T];
}
Return randomcode;
}
// Generate an image
Private void createimage (string checkcode)
{
Int iwidth = (INT) (checkcode. length * 11.5 );
System. Drawing. bitmap image = new system. Drawing. Bitmap (iwidth, 20 );
Graphics G = graphics. fromimage (image );
Font F = new system. Drawing. Font ("Arial", 10, system. Drawing. fontstyle. Bold );
Brush B = new system. Drawing. solidbrush (color. White );
// G. fillrectangle (new system. Drawing. solidbrush (color. Blue), 0, 0, image. Width, image. Height );
G. Clear (color. Blue );
G. drawstring (checkcode, F, B, 3, 3 );
// Pen blackpen = new pen (color. Black, 0 );
// Random Rand = new random ();
// For (INT I = 0; I <5; I ++)
//{
// Int y = Rand. Next (image. Height );
// G. drawline (blackpen, 0, Y, image. Width, y );
//}
System. Io. memorystream MS = new system. Io. memorystream ();
Image. Save (MS, system. Drawing. imaging. imageformat. JPEG );
Response. clearcontent ();
Response. contenttype = "image/JPEG ";
Response. binarywrite (Ms. toarray ());
G. Dispose ();
Image. Dispose ();
}
The client can write as follows:
1. Drag an image control to the webpage first. Pay attention to modifying its imageurl attribute to the server page.
2. Drag the corresponding control to enter the webpage and edit the button event:
Protected void button#click (Object sender, eventargs E)
{
If (this. textbox1.text = "rocket5725") & (this. textbox2.text = "rocket5725") & (this. textbox3.text = session ["checkcode"]. tostring ()))
{
Response. Redirect ("http://www.sina.com.cn ");
}
Else
Response. Write ("error ");
}