How to get Google's reCaptcha to work in Virtuemart 2.x (and stop those annoying spambots.)

Écrit par Super User le . Publié dans Blog

The following is a code guide to help people having problems implementing reCaptcha code in the Ask-a-question form from Virtuemart 2.x.

I've compiled this short tutorial since I was getting nowhere trying to get help from anywhere on the net and decided to attack it myself.

 


BEFORE YOU START

 

Disclaimer: USE THE INFO IN THIS ARTICLE AT YOUR OWN RISK. The information in this article is provided as a guideline to help with the implementation of Google's reCaptcha code. We are not responsible if you break your site.

 

Now, that being said, DON'T SKIP ANY STEPS:

1) Backup your Joomla/Virtuemart site. If you don't know how to do this with your ISP's control panel, lookup Akeeba Backup. It's a great product that allows you to backup your site using a simple GUI.

 

2) go to http://recaptcha.net and get a set of private and public keys by registering your site

 

3) make a copy and rename these two files:

  • components\com_virtuemart\controllers\productdetails.php to --> components\com_virtuemart\controllers\productdetails.php.backup

  • template\<your_template_name>\html\com_virtuemart\askquestion\form.php to --> template\<your_template_name>\html\com_virtuemart\askquestion\form.php.backup

This step is to make a backup of your current working files. Open these two files and make a copy of them in their respective directories. In other words, you should have :

components\com_virtuemart\controllers\productdetails.php

components\com_virtuemart\controllers\productdetails.php.backup

template\<your_template_name>\html\com_virtuemart\askquestion\form.php

template\<your_template_name>\html\com_virtuemart\askquestion\form.php.backup

 

** If your template is not using html overrides, you will find the form.php file in the components\com_virtuemart\views\askquestion\tmpldirectory. Copy and rename the files in that directory instead of the template one.

4). upload recaptchalib.php to both of those directories. (you will find this of Google's API site here:  recaptchalib). Download the .zip file and extract the files. only upload recaptchalib.php to the two directories above.

 

5). Edit the form.php file and insert this code around line 18 after the comments:

// CAPTCHA HEADER

require_once('recaptchalib.php');

$publickey = "your_public_key";

# the response from reCAPTCHA

$resp = null;

# the error code from reCAPTCHA, if any

$error = null;

// END CAPTCHA HEADER

 

Replace the your_public_key with the public key that was given to you when you registered your site for reCaptcha.

 

then towards line 137, find the <div class="submit"> line. paste the following code BEFORE that line:

<div class="reCaptcha">

   <?php

       echo recaptcha_get_html($publickey, $error);

   ?>

</div>

 

if your site uses SSL, the captcha will not display unless you change the echo line above to:

echo recaptcha_get_html($publickey, $error, $use_ssl = true);

 

Save the form.php file

 

6). Edit the productdetails.php file and find the "public function mailAskquestion () {" near line 60.

Right after that line, insert:

require_once('recaptchalib.php');

$privatekey = "your_private_key";

$resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], recaptcha_response_field"]);

if (!$resp->is_valid) {    

  // What happens when the CAPTCHA was entered incorrectly

  die ("reCAPTCHA validation failed.");

}

 

Replace the your_private_key by the one that was given to you when you registered your site for reCaptcha.

Save the file.

 

That should be it! the next time you "ask a question about this product" in Virtuemart, you should have the reCaptcha displayed.

 

Best of luck,

 

Chris