Search code examples
coldfusion

CFML reCAPTCHA v3 Issue


Im trying to use the code I have found and its not working properly it is always saying that I am a robot do you have any idea why this will not work? The Application.cfc has the site and secret key in it.

<script src="https://www.google.com/recaptcha/api.js?render=<cfoutput>#application.SiteKey#</cfoutput>"></script>


<cfif ISDEFINED('FORM.FirstName')> <!--- check if form was submitted and if so run code below --->

    <cfhttp url="https://www.google.com/recaptcha/api/siteverify?secret=#application.SecretKey#&response=#FORM['g-recaptcha-response']#" result="Response" />
    <cfset Return = deserializeJSON(Response.FileContent) />

    <cfif Return.success IS 'true' AND Return.score GT 0.0> <!--- check if true and if score is greater than 0.5. Run code below if all good. --->

        <cfoutput>Human: #FORM.FirstName# #FORM.LastName#</cfoutput>
        <!--- you can do database entry and/or email results here --->

    <cfelse>  <!--- if not a human, do this. I usually remove the else part completely, but if you need to do something with the robot, do it here.  --->

        Most likely a robot.

    </cfif>

<cfelse> <!--- show form --->

    <form method="post" action="/contact.cfm">  <!--- submit form back to itself --->
      First Name: <input name="FirstName" type="text"><br>
      Last Name: <input name="LastName" type="text"><br>
      <input name="submit" type="submit">
      <input name="g-recaptcha-response" id="g-recaptcha-response" type="hidden" /> <!--- javascript below gives this a value from google. --->
    </form>

    <script>
    grecaptcha.ready(function() {
        grecaptcha.execute('<cfoutput>#application.SiteKey#</cfoutput>', {action: 'homepage'})
            .then(function(token) {
                document.getElementById('g-recaptcha-response').value=token;
            });
        });
    </script>

</cfif>


Solution

  • This is how I was able to get the form working properly.

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
    <script src="https://www.google.com/recaptcha/api.js?render=YOUR SITE KEY"></script>
    <!-- contact form demo container -->
    <cfif ISDEFINED('FORM.name')> <!--- check if form was submitted and if so run code below --->
    
        <cfhttp url="https://www.google.com/recaptcha/api/siteverify?secret=#application.SecretKey#&response=#FORM['token']#" result="Response" />
        <cfset Return = deserializeJSON(Response.FileContent) />
    
        <cfif Return.success IS 'true' AND Return.score GT 0.5> <!--- check if true and if score is greater than 0.5. Run code below if all good. --->
    
           
    
        <cfelse>  <!--- if not a human, do this. I usually remove the else part completely, but if you need to do something with the robot, do it here.  --->
    
            
    
        </cfif>
    
    <cfelse>
    <section style="margin: 50px 20px;">
      <div style="max-width: 768px; margin: auto;">
        
        <!-- contact form -->
        <div class="card">
          <h2 class="card-header">Contact Form</h2>
          <div class="card-body">
            <form class="contact_form" method="post" action="contact.cfm">
    
              <!-- form fields -->
              <div class="row">
                <div class="col-md-6 form-group">
                  <input name="name" type="text" class="form-control" placeholder="Name" required>
                </div>
                <div class="col-md-6 form-group">
                  <input name="email" type="email" class="form-control" placeholder="Email" required>
                </div>
                <div class="col-md-6 form-group">
                  <input name="phone" type="text" class="form-control" placeholder="Phone" required>
                </div>
                <div class="col-md-6 form-group">
                  <input name="subject" type="text" class="form-control" placeholder="Subject" required>
                </div>
                <div class="col-12 form-group">
                  <textarea name="message" class="form-control" rows="5" placeholder="Message" required></textarea>
                </div>
    
                <!-- form message prompt -->
                <div class="row">
                  <div class="col-12">
                    <div class="contact_msg" style="display: none">
                      <p>Your message was sent.</p>
                    </div>
                  </div>
                </div>
    
                <div class="col-12">
                  <input type="submit" value="Submit Form" class="btn btn-success" name="post">
                </div>
    
                <!-- hidden reCaptcha token input -->
                <input type="hidden" id="token" name="token">
              </div>
    
            </form>
          </div>
        </div>
    
      </div>
    </section>
    <script>
      grecaptcha.ready(function() {
        grecaptcha.execute('YOUR SITE KEY', {action: 'homepage'}).then(function(token) {
           // console.log(token);
           document.getElementById("token").value = token;
        });
        // refresh token every minute to prevent expiration
        setInterval(function(){
          grecaptcha.execute('YOUR SITE KEY', {action: 'homepage'}).then(function(token) {
            console.log( 'refreshed token:', token );
            document.getElementById("token").value = token;
          });
        }, 60000);
    
      });
    </script>
    </cfif>
    
    <!-- References for the optional jQuery function to enhance end-user prompts -->
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>