Implement a common Verification Code Function

Source: Internet
Author: User

This function is essential for websites. I found it online and used some other people's code to get my project. A general functional module.

If you need this function, you can directly copy the following code.

 

Front-end UI

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "validatecode. aspx. cs" inherits = "ui_validatecode" %>

 

<! 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 runat = "server">

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>

<Title> Verification code implementation-by wangtao </title>

<SCRIPT type = "text/JavaScript" src = "../MISC/JS/CORE/jquery. js"> </SCRIPT>

</Head>

<Body>

<SCRIPT type = "text/JavaScript">

 

// Click the image to change the verification code

$ (Function (){

 

$ ("# Validatecode"). Click (function (){

VaR src = $ (this). ATTR ("src ");

SRC + = "? "+ Math. Random ();

$ (This). ATTR ("src", Src );

})

})

</SCRIPT>

<Form ID = "validatecodeform" runat = "server">

<Div>

<Input type = "text" id = "txtcode"/>

<P> <input type = "Submit" value = "verify"/> </P>

</Div>

</Form>

</Body>

</Html>

Background code: <% @ webhandler Language = "C #" class = "getvalidatecode" %> using system; using system. web; using system. drawing; public class getvalidatecode: ihttphandler {public void processrequest (httpcontext context) {// context. response. addheader = ""; context. response. cache. setcacheability (httpcacheability. nocache); context. response. clearcontent (); context. response. contenttype = "image/GIF"; // context. response. contenttype = "image/GIF"; this. createcheckcodeimage (generatecheckcode (context), context);} public bool isreusable {get {return false ;}} private string generatecheckcode (httpcontext context) {int number; char code; string checkcode = string. empty; system. random random = new random (); For (INT I = 0; I <4; I ++) {number = random. next (); If (Number % 2 = 0) code = (char) ('0' + (char) (Number % 10 )); else if (Number % 3 = 0) code = (char) ('A' + (char) (Number % 26); else code = (char) ('A' + (char) (Number % 26); checkcode + = code. tostring ();} context. response. cookies. add (New httpcookie ("checkcode", checkcode); // It can also be stored in the cookie. // context. session ["checkcode"] = checkcode; // context. session return checkcode;} private void createcheckcodeimage (string checkcode, httpcontext context) {If (checkcode = NULL | checkcode. trim () = string. empty) return; system. drawing. bitmap image = new system. drawing. bitmap (INT) math. ceiling (checkcode. length * 12.5), 22); graphics G = graphics. fromimage (image); try {// generate random generator random = new random (); // clear the image background color G. clear (color. white); // specifies the background noise line of the image. For (INT I = 0; I <25; I ++) {int X1 = random. next (image. width); int X2 = random. next (image. width); int Y1 = random. next (image. height); int y2 = random. next (image. height); G. drawline (new pen (color. silver), X1, Y1, X2, Y2);} Font font = new system. drawing. font ("Arial", 12, (system. drawing. fontstyle. bold | system. drawing. fontstyle. italic); system. drawing. drawing2d. lineargradientbrush brush = new system. drawing. drawing2d. lineargradientbrush (New rectangle (0, 0, image. width, image. height), color. blue, color. darkred, 1.2f, true); G. drawstring (checkcode, Font, brush, 2, 2); // foreground noise of the image to be painted for (INT I = 0; I <100; I ++) {int x = random. next (image. width); int y = random. next (image. height); image. setpixel (X, Y, color. fromargb (random. next ();} // draw the border line G of the image. drawrectangle (new pen (color. silver), 0, 0, image. width-1, image. height-1); system. io. memorystream MS = new system. io. memorystream (); image. save (MS, system. drawing. imaging. imageformat. GIF); // context. response. clearcontent (); // context. response. contenttype = "image/GIF"; context. response. binarywrite (Ms. toarray ();} finally {G. dispose (); image. dispose ();}}}

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.