Skip to main content
Inspiring
June 8, 2017
Question

Problem integrating Google reCaptcha v2 into a simple form

  • June 8, 2017
  • 0 replies
  • 819 views

I am at my wits' end attempting to integrate Google's recaptcha v2 with a simple HTML form. I hope someone can help.

In the <head> of the form page I have

    <script src="https://www.google.com/recaptcha/api.js" async defer></script>

as directed by Google.

Within the form I have

    <div class="g-recaptcha" data-sitekey="xxxxxxxxxxx"></div>

where xxxxxxxxxxx is the sitekey provided by Google. When the form is displayed, this is properly creating the recaptcha widget.

On the target page that processes the form submission, I have this:

    <cfset form.RecaptchaResponse = "">

    <cfif StructKeyExists(form, "g-recaptcha-response")>

           <cfset form.RecaptchaResponse = form["g-recaptcha-response"]>

    </cfif>

    <cfhttp url="https://www.google.com/recaptcha/api/siteverify" method="post" result="captchaResult">

      <cfhttpparam type="formfield" name="secret" value="zzzzzzzz">

      <cfhttpparam type="formfield" name="response" value="#form.RecaptchaResponse#">

      <cfhttpparam type="formfield" name="remoteip" value="#cgi.remote_addr#">

    </cfhttp>

    <cfset result = "#deserializeJSON(StructFind(variables.captchaResult, 'FileContent'))#">

    <cfif result.success is "false">

        <cflocation addtoken="no" URL="/">

    </cfif>

     ...start processing the submission.

where zzzzzzzz is the secret key provided by Google.

First I intentionally submit the form without responding to the captcha. I dump the variable captchaResult (a structure). Its 'FileContent' key is a JSON string, and in that string, 'Success' is 'false'. This submission redirects to the site's home page -- exactly as desired.

Next I submit the form correctly, including responding to the captcha. Dumping captchaResult and then aborting, I see that 'Success' is 'true'. In other words, Google is definitely passing this submission.

So now I reload everything and resubmit correctly, including responding to the captcha. ColdFusion replies with this on my target page:

   We're sorry...

    Element G is undefined in FORM.

    The error occurred on line 1.

What the #$% is going on?? Any suggestions will be gratefully and humbly accepted.

Message was edited by: Jordan Backler Forgot to include that I am using CF 10

This topic has been closed for replies.