博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

验证码

Posted on 2005-02-26 23:49  dalongzero  阅读(428)  评论(0编辑  收藏  举报
image.cfm
<!-- ////////////////////////////////////////////////
  Source Code Copyright by Mozier, blog.coolog.com
  Application: validateCode 
  Supported CF Version: CFMX
  File Name: image.cfm
  Created By: Mozier (Mozier at 163.com)
  Created Date: 2004/10/24
  Description: create validate code
  Version History: yyyy/mm/dd   Version    Comments
                   2004/10/24     1.0       created
  ///////////////////////////////////////////////////
 -->
<!---******************加载系统字体************************* --->
<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.white) />
  <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>
<!-- 输出图象到页面 -->
<cfcontent type="image/x-png" file="#path#code.png" />
<!-- 设置页面不被缓存 -->
<cfheader name="Pragma" value="no-cache" />
<cfheader name="Cache-Control" value="no-cache" />
<cfheader name="Expires" value="0" />


应用:
<img src="image.cfm" />


验证的时候将输入值和session.code比较,不相同则返回一个错误,比如:
<cfif CompareNoCase(session.code,FORM.validate) eq 0>
<cfset error="验证码正确" />
<cfelse>
<cfset error="验证码错误">
</cfif>
<cfoutput>#error#</cfoutput>


当然在此基础上还可以给验证码加随机背景色或随机雪花点之类的,就看你怎么去做了,只要肉眼能看清~

演示:
www.coolog.com/temp/authcode/test.cfm