ColdFusion 7 generate a verification code instance (CF verification code)

Source: Internet
Author: User
Document directory
  • Principle: point the image SRC to a CFM file, use the Java function in the CFM file to create an image that displays the verification code, and record the session variable. After you enter the verification code, compare it with the session variable!
  • Example:
  • Compare the verification code entered by the user:
  • The content of CAPTCHA. cfm is as follows:
Principle: point the image SRC to a CFM file, use the Java function in the CFM file to create an image that displays the verification code, and record the session variable. After you enter the verification code, compare it with the session variable! Example:

Enter the verification code: <input type = "text" name = "CAPTCHA">

Compare the verification code entered by the user:

<Cfif comparenocase (session. Code, form. CAPTCHA) NEQ 0>
<! --- Incorrect verification code --->
</Cfif>

The content of CAPTCHA. cfm is as follows:

<Cffunction name = "loadsystemfont" Access = "private" output = "false" returntype = "any">
<Cfargument name = "fontname" required = "no" type = "string" default = "Georgia"/>
<Cfargument name = "size" required = "no" type = "numeric" default = "18"/>
<Cfargument name = "style" required = "no" type = "string" default = "italic"/>
<Cfset var font = Createobject ("Java", "Java. AWT. Font")/>
<Cfreturn font. Decode ("# arguments. fontname #-# ucase (arguments. Style) #-# arguments. size #")/>
</Cffunction>
<! -- ***************** Generate random numbers and letters **************** * *** -->
<Cffunction name = "getrandomcode" Access = "private" returntype = "string" output = "false">
<Cfargument name = "length" type = "numeric" required = "no" default = "4"/>
<Cfset var charlist = "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 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 "/>
<Cfset var randomcode = ""/>
<Cfloop from = "1" to = "# arguments. Length #" Index = "I">
<Cfset thisnum = randrange (1, listlen (charlist, "|")/>
<Cfset thischar = listgetat (charlist, thisnum, "|")/>
<Cfset randomcode = listappend (randomcode, thischar, "")/>
</Cfloop>
<Cfset randomcode = Replace (randomcode, "", "", "all")/>
<Cfreturn randomcode/>
</Cffunction>
<! ************** ***************** -->
<Cffunction name = "createimage" Access = "private" returntype = "any" output = "false">
<Cfargument name = "width" type = "numeric" required = "no" default = "70"/>
<Cfargument name = "height" type = "numeric" required = "no" default = "25"/>
<! -- Create a Java class -->
<Cfset var image = Createobject ("Java", "Java. AWT. image. bufferedimage")/>
<Cfset var color = Createobject ("Java", "Java. AWT. Color")/>
<! -- Define the font style -->
<Cfset fontstyle = loadsystemfont ()/>
<! -- Create an image in memory -->
<Cfset image. INIT (javacast ("int", arguments. width), javacast ("int", arguments. Height), image. type_int_rgb)/>
<! -- Obtain the image -->
<Cfset G = image. getgraphics ()/>
<! -- Set the background color -->
<Cfset G. setcolor (color. Gray)/>
<Cfset G. fillrect (0, 0, javacast ("int", arguments. width), javacast ("int", arguments. Height)/>
<! -- Obtain a random id -->
<Cfset code = getrandomcode ()/>
<! -- Write the authentication code to the session for comparison -->
<Cfset session. Code = code/>
<! -- Display the authentication code to the image -->
<Cfset G. setcolor (color. Black)/>
<Cfset G. setfont (fontstyle)/>
<Cfset G. drawstring (Code, javacast ("int", 12), javacast ("int", 18)/>
<! -- Make the image take effect -->
<Cfset G. Dispose ()/>
<Cfreturn image/>
</Cffunction>

<Cfscript>
// Define the image storage path
Path = getdirectoryfrompath (expandpath ("*.*"));
Outputstream = Createobject ("Java", "Java. Io. fileoutputstream ");
ImageIO = Createobject ("Java", "javax. ImageIO. ImageIO ");
Outputstream. INIT ("Your pathpattern code.png ");
// Output the image to the local device.
ImageIO. Write (createimage (), "PNG", outputstream );
</Cfscript>
<! -- Set the page not to be cached -->
<Cfheader name = "Pragma" value = "no-Cache"/>
<Cfheader name = "cache-control" value = "no-Cache"/>
<Cfheader name = "expires" value = "0"/>
<Cfcontent type = "image/X-PNG" file = "Export path=code.png"/>

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.