Skip to content

Instantly share code, notes, and snippets.

@mhenke
Created December 27, 2010 21:11
Show Gist options
  • Save mhenke/756563 to your computer and use it in GitHub Desktop.
Save mhenke/756563 to your computer and use it in GitHub Desktop.
captcha custom tag
<!---
This will create two variables for the page too be outputted:
OUTPUT:
CAPTCHA_DISPLAY - captcha fields/image
CAPTCHA_ERROR - error message of captcha failure
blnIsBot - is this a bot
http://www.bennadel.com/blog/873-Using-CAPTCHA-In-ColdFusion-8.htm
--->
<!--- Kill extra output. --->
<cfsilent>
<cfparam name="attributes.captcha_show" type="boolean" default="false" />
<!--- Param FORM values --->
<cfparam name="caller.FORM.submitted" type="string" default="0" />
<cfparam name="caller.FORM.CAPTCHA_ERROR" type="string" default="" />
<cfif isDefined("caller.form.fieldnames")>
<cfset attributes.captcha_show = true />
</cfif>
<cfset blnIsBot = "true" />
<!--- Set a flag to see if this user is a bot or not. --->
<cfif not caller.FORM.submitted>
<cfset blnIsBot = "false" />
</cfif>
<!--- Check to see if the form has been SUBMITTED. --->
<cfif caller.FORM.submitted>
<cftry>
<!--- Decrypt the check value. --->
<cfset strCaptcha = Decrypt(caller.FORM.captcha_check,"bots-aint-sexy","CFMX_COMPAT","HEX") />
<cfif Compare(strCaptcha,caller.FORM.captcha) eq "0">
<cfset blnIsBot = false />
</cfif>
<!--- Catch any errors. --->
<cfcatch>
<!--- Make sure the bot flag is set. --->
<cfset blnIsBot = true />
</cfcatch>
</cftry>
</cfif>
<cfset string_list = "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,">
<cfset string_list = ucase(string_list) & lcase(string_list)>
<cfset arrValidChars = ListToArray(
string_list &
"0,1,2,3,4,5,6,7,8,9"
) />
<!--- Now, shuffle the array. --->
<cfset CreateObject("java","java.util.Collections").Shuffle(arrValidChars) />
<cfset strCaptcha = (arrValidChars[ 1 ] & arrValidChars[ 2 ] & arrValidChars[ 3 ] & arrValidChars[ 4 ] & arrValidChars[ 5 ] ) />
<cfset caller.FORM.captcha_check = Encrypt(strCaptcha,"bots-aint-sexy","CFMX_COMPAT","HEX") />
</cfsilent>
<cfoutput>
<cfsavecontent variable="caller.CAPTCHA_DISPLAY">
<input type="hidden" name="SUBMITTED" value="#attributes.captcha_show#" />
<cfif attributes.captcha_show>
<input type="hidden" name="captcha_check" value="#caller.FORM.captcha_check#" />
<p>
<cfimage action="captcha" height="75" width="300" text="#strCaptcha#" difficulty="medium" fonts="verdana,arial,times new roman,courier" fontsize="28" />
</p>
<label for="captcha">Please enter text in image:</label>
<input type="text" name="captcha" id="captcha" value="" />
<br />
</cfif>
</cfsavecontent>
<cfif blnIsBot>
<cfsavecontent variable="caller.CAPTCHA_ERROR">
<h3><strong>Please re-enter the text in the image.</strong><br><br></h3>
</cfsavecontent>
</cfif>
<cfset caller.blnIsBot = blnIsBot />
</cfoutput>
@mhenke
Copy link
Author

mhenke commented Dec 27, 2010

turn this into a udf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment