当前位置: 代码迷 >> ColdFusion >> Coldfusion 七 生成验证码实例(CF验证码)
  详细解决方案

Coldfusion 七 生成验证码实例(CF验证码)

热度:10272   发布时间:2013-02-26 00:00:00.0
Coldfusion 7 生成验证码实例(CF验证码)

原理: ?将图片的SRC指向一个CFM文件,在CFM文件中用JAVA功能创建一个显示验证码的图片,同时记录Session变量。当用户输入验证码后与Session变量进行比较!

示例:

请输入验证码: <input type="text" name="captcha"><img src="captcha.cfm" id="captchaImage" border="0">

比较用户输入的验证码:

<cfif comparenocase(session.code,form.captcha) neq 0>
<!---验证码不对--->
</cfif>

captcha.cfm内容如下:

<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>
<!-- ******************产生随机数字和字母******************** -->
<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" />
<!-- 创建JAVA类 -->
<cfset var image = createObject("java","java.awt.image.BufferedImage") />
<cfset var color=createObject("java","java.awt.Color") />
<!-- 定义字体样式 -->
<cfset fontStyle=loadSystemFont() />
<!-- 在内存中创建图象 -->
<cfset image.init(javaCast("int",arguments.width),javaCast("int",arguments.height),image.TYPE_INT_RGB) />
<!-- 获取图形 -->
<cfset g = image.getGraphics() />
<!-- 设定背景色 -->
<cfset g.setColor(color.gray) />
<cfset g.fillRect(0,0,javaCast("int",arguments.width),javaCast("int",arguments.height)) />
<!-- 获取随机产生的认证码 -->
<cfset code=getRandomCode() />
<!-- 将认证码写入SESSION用于比较 -->
<cfset session.code=code />
<!-- 将认证码显示到图象中 -->
<cfset g.setColor(color.black) />
<cfset g.setFont(fontStyle) />
<cfset g.drawString(code,javaCast("int",12),javaCast("int",18)) />
<!-- 使图象生效 -->
<cfset g.dispose() />
<cfreturn image />
</cffunction>

<cfscript>
//定义图像存放路径
path=getDirectoryFromPath(expandPath("*.*"));
outputStream = createObject("java","java.io.FileOutputStream");
imageIO = createObject("java","javax.imageio.ImageIO");
outputStream.init("#path#code.png");
// 输出图象到本地
imageIO.write(createImage(),"png",outputStream);
</cfscript>
<!-- 设置页面不被缓存 -->
<cfheader name="Pragma" value="no-cache" />
<cfheader name="Cache-Control" value="no-cache" />
<cfheader name="Expires" value="0" />
<cfcontent type="image/x-png" file="#path#code.png" />