December 21, 2020
Add Google recaptcha to Cold Fusion form
Comments
(2)
December 21, 2020
Add Google recaptcha to Cold Fusion form
Newbie 2 posts
Followers: 0 people
(2)

Does anyone know how to add a recaptcha to a Cold Fusion form? The form should not submit until the recaptcha has been checked off. I keep finding everything with PHP for this, but nothing with a Cold Fusion form. Here’s a simple form I created below. I’m not sure how to put the code in a special box here. I also left the Secret Key and Site key off of here on purpose.

<cfparam name=”FORM.username” default=”” type=”string” >
<cfparam name=”FORM.password” default=”” type=”string” >
<cfif structKeyExists(FORM,”submit”)>
<cfset recaptcha = FORM[“recaptcha”] >
<cfif len(recaptcha)>
<cfset googleUrl = “https://www.google.com/recaptcha/api/siteverify”>
<cfset secret = “Secret key”>
<cfset ipaddr = CGI.REMOTE_ADDR>
<cfset request_url = googleUrl & “?secret=” & secret & “&response=” & recaptcha & “&remoteip” & ipaddr>
<cfhttp url=”#request_url#” method=”get” timeout=”10″>
<cfset response = deserializeJSON(cfhttp.filecontent)>
<cfif response.success eq “YES”>
<!— Start validating the username/password here. —>
</cfif>
</cfif>
</cfif>
<!DOCTYPE html>
<html>
<head>
<script src=’https://www.google.com/recaptcha/api.js’></script>
</head>
<body>
<cfform method=”post”>
UserName:
<input type=”text” name=”username”>
<br><br>
Password:
<input type=”password” name=”password”>
<br><br>
<div class=”g-recaptcha” data-sitekey=”site key”></div>
            <cfif isDefined(“form.recaptcha”)>
<input type=”submit” value=”Submit” name=”submit”>
            <cfelseif IsNotDefined(“form.recaptcha”)>
         <input type=”submit” value=”Submit” disabled name=”submit”>
    </cfif>
</cfform>
</body>
</html>
2 Comments
2021-01-05 13:52:40
2021-01-05 13:52:40

Jamie?

Like
2020-12-30 17:58:11
2020-12-30 17:58:11

Jamie, to answer your first question, yes it is possible to leverage google’s recaptcha in a CF form.

Of course, it’s really a javascript library, but many do want to integrate it with CFML more specifically, just like you say you find discussions of integrating with PHP and such. And while you may be looking for help to diagnose whatever may be amiss with your code, and someone may well want to jump in to diagnose that, I will offer a different solution.

There are multiple existing CFML solutions to leveraging recpatcha, and you can either switch to using one of those (like I do), or you can at least look at their code to see how it works and why yours does not.

The one I use is this: https://github.com/jeffpratt/ColdFusion-No-CAPTCHA-reCAPTCHA. It’s a simple CFML custom tag. All you have to do is put your Google keys for your repatcha account into the recaptcha.cfml file. The repo explains that process and offers that code.

The repo also offers a sample to test calling it, and it’s just a few lines of code that you could roll into your own form (to call the custom tag, and test its results). Again, it’s the one I use and it does work (even in CF2018).

There are still others, which you or others may want to look into also (I have not tested them so don’t know if they still work). Most are indeed years old, but the recaptcha stuff has not changed much. See the following (and any comments in blog posts or issues in github repos):

Let us know if one of those gets you going.

Like
Add Comment