Solved

How do I use ReCaptcha with ColdFusion?

Posted on 2008-11-01
9
2,214 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:PierceWeb
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 10

Expert Comment

by:Mr_Nil
ID: 22863777
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
 
LVL 36

Accepted Solution

by:
SidFishes earned 500 total points
ID: 22867964
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
 
LVL 36

Expert Comment

by:SidFishes
ID: 22867995
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
 
LVL 2

Author Closing Comment

by:PierceWeb
ID: 31512353
SidFishes, Thanks for going the extra mile and including some CF example code. I appreciate your assistance! Thanks again. Tom
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 2

Author Comment

by:PierceWeb
ID: 22869734
Thanks for your assistance guys! I appreciate it!
0
 
LVL 10

Expert Comment

by:Mr_Nil
ID: 22874690
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
 

Expert Comment

by:zevaca
ID: 23170454
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
 

Expert Comment

by:zevaca
ID: 23170460
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
 

Expert Comment

by:zevaca
ID: 23189206
Please delete the above comment....I posted it in the wrong place.  Thanks.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now