Jump to content

Fix to upgrade Google Recaptcha

Recommended Posts

Okay this is one way of upgrading to v2 of recaptcha.

Go here https://github.com/google/recaptcha and scroll down to Direct Download (assuming you don't use composer) and download the package. Once downloaded insert it into:  core/lib/recaptcha-master

Once you have done this go to core/modules/registration/registration.php

# Replace or comment out
require_once CORE_LIB_PATH.'/recaptcha/recaptchalib.php';

# With
require_once CORE_LIB_PATH.'/recaptcha-master/src/autoload.php';

In that same file, scroll down to VerifyData

# Replace all code relating to captcha check with

  // start the error array as false, we dont have any errors yet.
  $error = false;

  // set the configurations for the service to work.
  $secret = Config::Get('RECAPTCHA_PRIVATE_KEY');
  $remoteIp = $_SERVER["REMOTE_ADDR"];
  $gRecaptchaResponse = $_POST['g-recaptcha-response'];
  $recaptcha = new \ReCaptcha\ReCaptcha($secret); 

  // Call the google service and verify the answer provided.
  $resp = $recaptcha->verify($gRecaptchaResponse, $remoteIp);

  // If Response was a success
  if ($resp->isSuccess())
      // No errors; continue
      $this->set('captcha_error', '');
      // Set error as true and show error on front-end
      $error = true;
      $this->set('captcha_error', true);

And finally on your registration_mainform.tpl 

<div class="g-recaptcha" data-sitekey="<?php echo Config::Get('RECAPTCHA_PUBLIC_KEY'); ?>"></div>
<script type="text/javascript"

<!-- Recaptcha V2 -->
<?php if($captcha_error == true)
    echo '<p class="error">Please verify you are human!</p>';

you can make a function for that like v1 but that's up to you. Let me know if this helps you! 

Share this post

Link to post
Share on other sites

Thanks for this, I split it out of the other topic and pinned it.

Share this post

Link to post
Share on other sites

I tried to follow this fix, but it doesn't work, or more likely I am making the edits wrong. You talk about "then scroll down to the "VerifyData" section, but there is 2 sections I see for that and when I make the edits it breaks the reg pages.

Can you write this how-to more clearly please? Show exactly what and where?

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now