One client wanted simple spam prevention device on the customer registration form of his Zencart store and while I looked at the plugins available on the Zencart website, as I had done some hefty customisation to the client’s Zencart theme already, I was worried that some of the plugins would overwrite all my hard work.
So I did it from scratch and here’s how:
1) Edit your YOUR_TEMPLATE/templates/tpl_module_create_account.php
This is the form that is used on both the login & create account pages. You need to add the actual input field here:
Which should give you something like this on your form:
Now we need to get it to work. There are two places that Zencart does its validation – within the javascript (which pops up the alert) and within the php which is the fallback if the customer somehow circumvents the javascript.
Lets look at the php first;
2) Edit your modules\create_account.php
First we need to grab a reference to our new fields so at about line 83 add the following:
//-------------- BOF Additional Captcha code LJ 31/1/13 1 of 2 -------------------------//
$captcha = zen_db_prepare_input($_POST['captcha']);
//-------------- EOF Additional Captcha code LJ 31/1/13 1 of 2 -------------------------//
Then we need to flag an error if the answer isn’t 10 so add the following at around line 253:
//-------------- BOF Additional Captcha code LJ 31/1/13 2 of 2 -------------------------//
if ($captcha=="") {
$error = true;
$messageStack->add('create_account', "You must enter the answer to the question");
} elseif ($captcha!="10") {
$error = true;
$messageStack->add('create_account', "Your answer to the question is incorrect");
}
//-------------- EOF Additional Captcha code LJ 31/1/13 2 of 2 -------------------------//
So far so good – this will alert your user if they haven’t filled anything or have got the wrong answer, but only after they have submitted the form.
But it would be nice to give them more of a heads up if they made a mistake before submitting so we need to add some javascript.
3) Edit your modules\pages\create_account\jscript_form_check.php
(Yes I know its php but it does produce a javascript function I promise)
First we want to add a little function to check the captcha:
At around line 121 add the following:
/*----------------- BOF new captcha test LJ 31/1/13 1 of 2-----------------------*/
function check_captcha(field_name, field_answer, message) {
if (form.elements[field_name] && (form.elements[field_name].type != "hidden")) {
var field_value = form.elements[field_name].value;
if (field_value.length != 0 && field_value != field_answer) {
error_message = error_message + "* " + message +"\n";
error = true;
}
}
}
/*----------------- EOF new captcha test LJ 31/1/13 1 of 2-----------------------*/
and at around line 184 add these lines:
check_input("captcha",1, "");
check_captcha("captcha","10", "");
You will also need to make these changes to the following file: modules\pages\login\jscript_form_check.php as that form has a slightly different javascript function.
Its not particularly elegant and I’ve hardcoded the answer to the spam question in several places, but it should only take 15 minutes to implement and if it prevents you getting all those spam mesages then I’m sure its worth it!
You are a legend. I saw this solution on some site and thought to myself this is an elegant solution for my site and now you have done it.
It seems to be working.
Can you please tell me how I do this for my Contact Us page. Whic his really where i need it.
Awesome thank you for sharing, I use checkout without account and this worked perfectly to stop those annoying spam accounts without damaging the option to COWOA.
thanks for this solution .. Have been looking for a simple way to use it on ask a question & create account ,so will give it a try sometime
Thanks – just added to the site, hopefully will stop the spam!
H i just wanna know. Is this code only for one question?
I mean every one who register will get the same question of 5+5? … Or u can put it to make different questions. Like if u refresh or make a mistake with the answer, do u get another question of 6+6? Or watever
Hi Pinky,
No, there is just the one question – it was only meant to be a really quick & dirty spam trap to prevent some automatic spam bots!
Can this code also be added to the Contact Us section? If so, can you tell us where to put it? Thanks!
Hello, I’m looking for a way to modify the Contact Us code to add your anti spam question, would you be able to let me know where to insert your question code please?
Cheers Greg
its not working. customer can leave it blank and proceed. How to make it required?
Adding this functionality to the contact us form is relatively simple. Two file changes:
your_template/templates/tpl_contact_us_default.php
1 change – add lines:
What is 5+5?
near bottom of contact form.
modules/pages/contact_us/header_php.php
3 changes:
add 2 lines around line 30 after:
$enquiry = zen_db_prepare_input(strip_tags($_POST[‘enquiry’]));
lines:
$captcha = zen_db_prepare_input($_POST[‘captcha’]);
$include_list = array(“a”,”A”);
then around line 40 change this line:
if ($zc_validate_email and !empty($enquiry) && $error == FALSE)
to this:
if ($zc_validate_email and !empty($enquiry) and !empty($name) and !empty($captcha) and (in_array($captcha, $include_list)) && $error == FALSE)
towards the end of the file after this
else {
$error = true;
add lines:
//————– BOF Additional Captcha code LJ 31/1/13 2 of 2 ————————-//
if ($captcha==””) {
$messageStack->add(‘contact_us’, “You must enter the answer to the question”); } elseif (!in_array($captcha, $include_list)) {
$messageStack->add(‘contact_us’, “Your answer to the question is incorrect”); }
Here’s how to add it to ZC contact form:
http://www.mzoo.org/2013/11/12/adding-simple-captcha-spam-trap-to-zen-cart-contact-form/
Thank You !