How do I use ReCaptcha with ColdFusion?

I would like to use ReCaptcha on a member sign-up page of a ColdFusion Web site, but am having no luck getting it to work properly.

* I downloaded the ColdFusion script from: http://recaptcha.riaforge.org/
* I secured a public and private recaptcha key from http://recaptcha.net/ and placed it the appropriate location in the script.
* When using the following code, I get no output but a blank screen, (i.e. no recaptcha images).

Can anyone assist me on getting this to work?

Thank you very much,

Thom
<cfsetting enablecfoutputonly="true">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
		<html>
		<body>
 
		<cf_recaptcha action="check"
			privateKey="ABC123etc..."
			publicKey="DEF456etc...">
 
		<cfif isDefined("form.submit")>
			<cfoutput>recaptcha says #form.recaptcha#</cfoutput>
		</cfif>
 
		<cfform>
		
			<cf_recaptcha
			privateKey="ABC123etc..."
			publicKey="DEF456etc...">
 
			<cfinput type="submit" name="submit">
 
		</cfform>
 
		</body>
		</html>
 
<cfscript>
	CHALLENGE_URL = "http://api.recaptcha.net";
	SSL_CHALLENGE_URL = "https://api-secure.recaptcha.net";
	VERIFY_URL = "http://api-verify.recaptcha.net/verify";
</cfscript>
 
<cfif not structKeyExists(attributes, "publicKey")>
	<cfthrow type="RECAPTCHA_ATTRIBUTE"
		message="recaptcha: required attribute 'publicKey' is missing">
</cfif>
 
<cfif not structKeyExists(attributes, "privateKey")>
	<cfthrow type="RECAPTCHA_ATTRIBUTE"
		message="recaptcha: required attribute 'privateKey' is missing">
</cfif>
 
<cftry>
	<cfset sInvalidAttr="action not render|check">
	<cfparam name="attributes.action" type="regex" pattern="(render|check)" default="render">
	<cfset sInvalidAttr="ssl not true|false">
	<cfparam name="attributes.ssl" type="boolean" default="false">
	<cfset sInvalidAttr="theme not red|white|blackglass">
	<cfparam name="attributes.theme" type="regex" pattern="(red|white|blackglass)" default="red">
	<cfset sInvalidAttr="tabIndex not numeric">
	<cfparam name="attributes.tabIndex" type="numeric" default="0">
<cfcatch type="any">
	<cfthrow type="RECAPTCHA_ATTRIBUTE"
		message="recaptcha: attribute #sInvalidAttr#">
</cfcatch>
</cftry>
 
<cfif isDefined("form.recaptcha_challenge_field") and isDefined("form.recaptcha_response_field")>
 
	<cftry>
		<cfhttp url="#VERIFY_URL#" method="post" timeout="5" throwonerror="true">
			<cfhttpparam type="formfield" name="privatekey" value="#attributes.privateKey#">
			<cfhttpparam type="formfield" name="remoteip" value="#cgi.REMOTE_ADDR#">
			<cfhttpparam type="formfield" name="challenge" value="#form.recaptcha_challenge_field#">
			<cfhttpparam type="formfield" name="response" value="#form.recaptcha_response_field#">
		</cfhttp>
	<cfcatch>
		<cfthrow  type="RECAPTCHA_NO_SERVICE"
			message="recaptcha: unable to contact recaptcha verification service on url '#VERIFY_URL#'">
	</cfcatch>
	</cftry>
 
	<cfset aResponse = listToArray(cfhttp.fileContent, chr(10))>
	<cfset form.recaptcha = aResponse[1]>
	<cfset structDelete(form, "recaptcha_challenge_field")>
	<cfset structDelete(form, "recaptcha_response_field")>
 
	<cfif aResponse[1] eq "false" and aResponse[2] neq "incorrect-captcha-sol">
		<cfthrow type="RECAPTCHA_VERIFICATION_FAILURE"
			message="recaptcha: the verification service responded with error '#aResponse[2]#'. See http://recaptcha.net/apidocs/captcha/ for error meanings.">
	</cfif>
 
<cfelse>
 
	<cfset form.recaptcha = false>
 
</cfif>
 
<cfif attributes.action eq "render">
 
	<cfif attributes.ssl>
		<cfset challengeURL = SSL_CHALLENGE_URL>
	<cfelse>
		<cfset challengeURL = CHALLENGE_URL>
	</cfif>
 
	<cfoutput>
	<script type="text/javascript">
	<!--
		var RecaptchaOptions = {
		   theme : '#attributes.theme#',
		   tabindex : #attributes.tabIndex#
		};
	//-->
	</script>
	<script type="text/javascript"
	   src="#challengeURL#/challenge?k=#attributes.publicKey#">
	</script>
	<noscript>
	   <iframe src="#challengeURL#/noscript?k=#attributes.publicKey#"
	       height="300" width="500" frameborder="0"></iframe><br>
	   <textarea name="recaptcha_challenge_field" rows="3" cols="40">
	   </textarea>
	   <input type="hidden" name="recaptcha_response_field"
	       value="manual_challenge">
	</noscript>
	</cfoutput>
 
</cfif>

Open in new window

LVL 2
PierceWebAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mr_NilCommented:
Have you used something like Firebug's net tab or Fiddler to check to see what is going to and coming back from recaptcha.net?

Is that recaptcha code included in the same page as the form?  If it is - take it out of there. The recaptcha code should be in a separate file called recaptcha.cfm in the same folder as your test page or in a folder on your custom tag path as specified in the coldfusion administrator.

0
SidFishesCommented:
here's the basic code to use. It uses 2 pages. I'm not sure if it works with cfform but it should. You'd have to add an action for it to work or modify the code to deal with a post back to the same page.

as noted by m.Nil "The recaptcha code should be in a separate file called recaptcha.cfm in the same folder as your test page or in a folder on your custom tag path as specified in the coldfusion administrator."

also note that the code must be used on the same domain that the key was created for (ie: can't use it on a local dev server)




----------------
index.cfm
----------------
 
<cfparam name="url.recaptcha" default="">
<cfif url.recaptcha eq "true">
	Thanks for your submission. 
<cfelse>
	<form method="POST"
		enctype="multipart/form-data"
		action="formAction.cfm">
		<cfif url.recaptcha eq "false">
			Captcha Authentication Failed
		</cfif>
		<cf_recaptcha
			privateKey="XXXXXXXXXX"
			publicKey="XXXXXXXXXXX"
			theme="white">
		<input type="submit"  value="submit">
	</form>
</cfif>
----------------
formAction.cfm
----------------
<cf_recaptcha
	privateKey="XXXXXXXXX"
	publicKey="XXXXXXXXXX"
	action="check">
<cfif form.recaptcha eq "true">
	<!--- do form submit --->
	Submit code here
	<!--- go back to calling page with TRUE --->
	<cflocation 
		url="index.cfm?recaptcha=#form.recaptcha#" 
		addtoken="no">
<cfelse>
	<!--- go back to calling page with FALSE --->
	<cflocation 
		url="index.cfm?recaptcha=#form.recaptcha#" 
		addtoken="no">
</cfif>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SidFishesCommented:
actually formAction could be simplified to the following
----------------
formAction.cfm
----------------
<cf_recaptcha
	privateKey="XXXXXXXXX"
	publicKey="XXXXXXXXXX"
	action="check">
<cfif form.recaptcha eq "true">
	<!--- do form submit --->
	Submit code here
</cfif>
<!--- go back to calling page with TRUE or FALSE --->
<cflocation 
	url="index.cfm?recaptcha=#form.recaptcha#" 
	addtoken="no">

Open in new window

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

PierceWebAuthor Commented:
SidFishes, Thanks for going the extra mile and including some CF example code. I appreciate your assistance! Thanks again. Tom
0
PierceWebAuthor Commented:
Thanks for your assistance guys! I appreciate it!
0
Mr_NilCommented:
umm - you didn't actually need to split it out to multiple pages.

The test page that you originally posted is a self submitting form ie. it submits to itself.  As soon as you put the recaptcha tag in its own file (as it was supplied from RIAForge) it would have worked.
0
zevacaCommented:
I get this error from the code above:
recaptcha: attribute action not render|check  
 
 
The error occurred in C:\CFusionMX\CustomTags\recaptcha.cfm: line 30
 
28 : <cfcatch type="any">
29 :       <cfthrow type="RECAPTCHA_ATTRIBUTE"
30 :             message="recaptcha: attribute #sInvalidAttr#">
31 : </cfcatch>
32 : </cftry>

 
This is suppose to render it:
<cfparam name="url.recaptcha" default="">
<cfif url.recaptcha eq "true">
      Thanks for your submission.
<cfelse>
      
        <form action="contact.cfm" method="post"  enctype="multipart/form-data" name="info" target="_self" onSubmit="MM_validateForm('Name','','R','CompanyName','','R','Address','','R','State','','R','Zip','','R','Phone','','R','Email','','RisEmail','NameOfChild','','R');return document.MM_returnValue">
          <table width="500" border="0" cellspacing="0" cellpadding="0" align="center">
            <tr>
              <td width="40%"><b><font face="Arial, Helvetica, sans-serif" size="2">Your
                Name: *</font></b></td>
              <td width="60%">
                <input type="text" name="Name">
              </td>
            </tr>
            <tr>
              <td width="40%"><b><font face="Arial, Helvetica, sans-serif" size="2">Company
                Name: *</font></b></td>
              <td width="60%">
                <input type="text" name="CompanyName">
              </td>
            </tr>
            <tr>
              <td width="40%"><b><font face="Arial, Helvetica, sans-serif" size="2">Address:
                * </font></b></td>
              <td width="60%">
                <input type="text" name="Address">
              </td>
            </tr>
            <tr>
              <td width="40%"><b><font face="Arial, Helvetica, sans-serif" size="2">Address
                Cont:</font></b></td>
              <td width="60%">
                <input type="text" name="AddressCont">
              </td>
            </tr>
            <tr>
              <td width="40%"><b><font face="Arial, Helvetica, sans-serif" size="2">City: *
                 </font></b></td>
              <td width="60%">
                <input type="text" name="City">
              </td>
            </tr>
            <tr>
              <td width="40%"><b><font face="Arial, Helvetica, sans-serif" size="2">State:
                * </font></b></td>
              <td width="60%">
                <input type="text" name="State">
              </td>
            </tr>
            <tr>
              <td width="40%"><b><font face="Arial, Helvetica, sans-serif" size="2">Zip:
                * </font></b></td>
              <td width="60%">
                <input type="text" name="Zip">
              </td>
            </tr>
            <tr>
              <td width="40%"><b><font face="Arial, Helvetica, sans-serif" size="2">Phone:
                * </font></b></td>
              <td width="60%">
                <input type="text" name="Phone">
              </td>
            </tr>
            <tr>
              <td width="40%"><b><font face="Arial, Helvetica, sans-serif" size="2">Your
                E-mail: *</font></b></td>
              <td width="60%">
                <input type="text" name="Email">
              </td>
            </tr>
            <tr>
              <td width="40%"><b><font face="Arial, Helvetica, sans-serif" size="2">Name
                Of Child You Are Requesting Info On: *</font></b></td>
              <td width="60%">
                <input type="text" name="NameOfChild">
              </td>
            </tr>
            <tr>
              <td width="40%"><b><font face="Arial, Helvetica, sans-serif" size="2">Additional
                Comments:</font></b></td>
              <td width="60%">
                <textarea name="Comments" rows="3" cols="20"></textarea>
              </td>
            </tr>
            <tr>
              <td width="40%" height="60" valign="bottom">
                    <cfif url.recaptcha eq "false">
                  Captcha Authentication Failed
            </cfif>
            <cf_recaptcha
                  privateKey="6LdLPAQAAAAAALYXZMnUGnLgd6Lhr6TagnrtAeeL"
                  publicKey="6LdLPAQAAAAAAM_zO7RxTTesO6VISBMF9RtlvJz0"
                  action="render">
                <input type="submit" name="Submit" value="Submit Request">
              </td>
              <td width="60%" height="60">&nbsp;</td>
            </tr>
          </table>
        </form>
      
</cfif>
This is supposed to check it:
<cf_recaptcha
      privateKey="6LdLPAQAAAAAALYXZMnUGnLgd6Lhr6TagnrtAeeL"
      publicKey="6LdLPAQAAAAAAM_zO7RxTTesO6VISBMF9RtlvJz0"
      action="check">
<cfif form.recaptcha eq "true">
      <!--- do form submit --->
      <!--- ============================================================ --->
 <cfmail to="info@kidshollywood.com"
         from="#Email#"  
           type="HTML"  
           subject="Information request">
          <table width="500" border="0" cellspacing="0" cellpadding="0" align="center">
            <tr>
              <td width="40%"><b><font face="Arial, Helvetica, sans-serif" size="2">Your Name: </font></b></td>
              <td width="60%">#Name#</td>
            </tr>
            <tr>
              <td width="40%"><b><font face="Arial, Helvetica, sans-serif" size="2">Company Name: </font></b></td>
              <td width="60%">#CompanyName#</td>
            </tr>
            <tr>
              <td width="40%"><b><font face="Arial, Helvetica, sans-serif" size="2">Address: </font></b></td>
              <td width="60%">#Address#</td>
            </tr>
            <tr>
              <td width="40%"><b><font face="Arial, Helvetica, sans-serif" size="2">Address Cont:</font></b></td>
              <td width="60%">#AddressCont#</td>
            </tr>
            <tr>
              <td width="40%"><b><font face="Arial, Helvetica, sans-serif" size="2">City: </font></b></td>
              <td width="60%">#City#</td>
            </tr>
            <tr>
              <td width="40%"><b><font face="Arial, Helvetica, sans-serif" size="2">State: </font></b></td>
              <td width="60%">#State#</td>
            </tr>
            <tr>
              <td width="40%"><b><font face="Arial, Helvetica, sans-serif" size="2">Zip: </font></b></td>
              <td width="60%">#Zip#
              </td>
            </tr>
            <tr>
              <td width="40%"><b><font face="Arial, Helvetica, sans-serif" size="2">Phone: </font></b></td>
              <td width="60%">#Phone#
              </td>
            </tr>
            <tr>
              <td width="40%"><b><font face="Arial, Helvetica, sans-serif" size="2">
                E-mail: </font></b></td>
              <td width="60%">#Email#</td>
            </tr>
            <tr>
              <td width="40%"><b><font face="Arial, Helvetica, sans-serif" size="2">Name Of Child You Are Requesting Info On: </font></b></td>
              <td width="60%">
                #NameOfChild#</td>
            </tr>
            <tr>
              <td width="40%"><b><font face="Arial, Helvetica, sans-serif" size="2">Additional Comments:</font></b></td>
              <td width="60%">#Comments#</td>
            </tr>
           
          </table>
       </cfmail>
<!--- ============================================================ --->
      <!--- go back to calling page with TRUE --->
      <!--- <cflocation
            url="private.cfm?recaptcha=#form.recaptcha#"
            addtoken="no"> --->
<cfelse>
      <!--- go back to calling page with FALSE --->
      <!--- <cflocation
            url="private.cfm?recaptcha=#form.recaptcha#"
            addtoken="no"> --->
</cfif>
The cflocation is commented out so I can see any errors that pop up.
0
zevacaCommented:
oh and this is the recaptcha code:
<cfsetting enablecfoutputonly="true">
 
<cfscript>
      CHALLENGE_URL = "http://api.recaptcha.net";
      SSL_CHALLENGE_URL = "https://api-secure.recaptcha.net";
      VERIFY_URL = "http://api-verify.recaptcha.net/verify";
</cfscript>
 
<cfif not structKeyExists(attributes, "publicKey")>
      <cfthrow type="RECAPTCHA_ATTRIBUTE"
            message="recaptcha: required attribute 'publicKey' is missing">
</cfif>
 
<cfif not structKeyExists(attributes, "privateKey")>
      <cfthrow type="RECAPTCHA_ATTRIBUTE"
            message="recaptcha: required attribute 'privateKey' is missing">
</cfif>
 
<cftry>
      <cfset sInvalidAttr="action not render|check">
      <cfparam name="attributes.action" type="regex" default="render">
      <cfset sInvalidAttr="ssl not true|false">
      <cfparam name="attributes.ssl" type="boolean" default="false">
      <cfset sInvalidAttr="theme not red|white|blackglass">
      <cfparam name="attributes.theme" type="regex" default="red">
      <cfset sInvalidAttr="tabIndex not numeric">
      <cfparam name="attributes.tabIndex" type="numeric" default="0">
<cfcatch type="any">
      <cfthrow type="RECAPTCHA_ATTRIBUTE"
            message="recaptcha: attribute #sInvalidAttr#">
</cfcatch>
</cftry>
 
<cfif isDefined("form.recaptcha_challenge_field") and isDefined("form.recaptcha_response_field")>
 
      <cftry>
            <cfhttp url="#VERIFY_URL#" method="post" timeout="5" throwonerror="true">
                  <cfhttpparam type="formfield" name="privatekey" value="#attributes.privateKey#">
                  <cfhttpparam type="formfield" name="remoteip" value="#cgi.REMOTE_ADDR#">
                  <cfhttpparam type="formfield" name="challenge" value="#form.recaptcha_challenge_field#">
                  <cfhttpparam type="formfield" name="response" value="#form.recaptcha_response_field#">
            </cfhttp>
      <cfcatch>
            <cfthrow  type="RECAPTCHA_NO_SERVICE"
                  message="recaptcha: unable to contact recaptcha verification service on url '#VERIFY_URL#'">
      </cfcatch>
      </cftry>
 
      <cfset aResponse = listToArray(cfhttp.fileContent, chr(10))>
      <cfset form.recaptcha = aResponse[1]>
      <cfset structDelete(form, "recaptcha_challenge_field")>
      <cfset structDelete(form, "recaptcha_response_field")>
 
      <cfif aResponse[1] eq "false" and aResponse[2] neq "incorrect-captcha-sol">
            <cfthrow type="RECAPTCHA_VERIFICATION_FAILURE"
                  message="recaptcha: the verification service responded with error '#aResponse[2]#'. See http://recaptcha.net/apidocs/captcha/ for error meanings.">
      </cfif>
 
<cfelse>
 
      <cfset form.recaptcha = false>
 
</cfif>
 
<cfif attributes.action eq "render">
 
      <cfif attributes.ssl>
            <cfset challengeURL = SSL_CHALLENGE_URL>
      <cfelse>
            <cfset challengeURL = CHALLENGE_URL>
      </cfif>
 
      <cfoutput>
      <script type="text/javascript">
      <!--
            var RecaptchaOptions = {
               theme : '#attributes.theme#',
               tabindex : #attributes.tabIndex#
            };
      //-->
      </script>
      <script type="text/javascript"
         src="#challengeURL#/challenge?k=#attributes.publicKey#">
      </script>
      <noscript>
         <iframe src="#challengeURL#/noscript?k=#attributes.publicKey#"
             height="300" width="500" frameborder="0"></iframe><br>
         <textarea name="recaptcha_challenge_field" rows="3" cols="40">
         </textarea>
         <input type="hidden" name="recaptcha_response_field"
             value="manual_challenge">
      </noscript>
      </cfoutput>
 
</cfif>
0
zevacaCommented:
Please delete the above comment....I posted it in the wrong place.  Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ColdFusion Language

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.