Adding reCaptcha to ZenCart Contact us form
If you got too many spam messages from your ZenCart Contact us page and you ould like to add reCaptcha script, here you will find the steps you have to do to make it work.
First of all you have to copy the ‘recaptchalib.php’ file to server wherever you like inside www/includes folder but remember this location. I’ve uploaded it to
www/includes/modules/pages/contuct_us
and if you trust me, copy it at the same location to ease the later usage.
Then go open and edit this file :
includes\modules\pages\contact_us\header_php.php
Find this line:
if (isset($_GET['action']) && ($_GET['action'] == 'send')) {
Right under it add this code:
require_once('recaptchalib.php'); $privatekey = "PRIVATE_KEY_HERE"; $resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]); if ( !$resp->is_valid) { $error = true; $messageStack->add('contact', "Wrong reCaptcha words. Please retry !"); }This is the code received from reCaptcha, changed to work with ZenCart. When user enters the wrong words they will receive an error message that you can customize as you want.
The next step is to find the line containing this code:
if ($zc_validate_email and !empty($enquiry) ) {and add this code at the end:
and $resp->is_validresulting this final line:
if ($zc_validate_email and !empty($enquiry) and !empty($name) and $resp->is_valid) {Save and close this file as we are done with it.
Next we will handle the template file
includes\templates\{YOUR_TEMPLATE}\templates\tpl_contact_us_default.phpHere we have to add the folowing code before the closing of fieldset tag: ‘</fieldset>’
<?php require_once('includes/modules/pages/contact_us/recaptchalib.php'); $publickey = "PUBLIC_KEY_HERE"; echo recaptcha_get_html($publickey); ?>Of course that PRIVATE_KEY_HERE and PUBLIC_KEY_HERE will be replaced by the keys delivered by reCaptcha for your domain.
Save it and from this moment your Zencart Contact page will be protected by reCaptcha.
