Zen Cart 1.5.1 comes with a CSS-driven anti-spam trap which adds a non-displayed field to the contact form, which robots would fill out, but not humans. After a while it seemed to not be enough so I initially added a Google reCaptcha module, but it was really annoying to use, so am trying this simple SPAM trap as explained by Kinetic Pulse.
I love acronyms and captcha is a really cool one, which stands for “Completely Automated Public Turing test to tell Computers and Humans Apart”. Nice.
Anyway the Kinetic Pulse tutorial applies to the create account and write review forms, so here’s how to do it in the Zen Cart contact form.
Adding this functionality to the ZC contact us form is relatively simple. Two file changes:
your_template/templates/tpl_contact_us_default.php
1 change – add lines:
<label for="captcha">What is 5+5?</label>
<?php echo zen_draw_input_field('captcha','', 'id="captcha"'); ?>
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']));
add two lines:
$captcha = zen_db_prepare_input($_POST['captcha']);
$include_list = array('ten', 'TEN', '10', 'Ten');
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', "You must enter the answer to the question"); } elseif (!in_array($captcha, $include_list)) {
$messageStack->add('contact', "Your answer to the question is incorrect"); }
.
Thats it. It has stopped the barrage of Russian Bot Spam so far. Hope it helps.
two notes:
$include_list = array(“a”,”A”); should be:
$include_list = array(“ten”,”10″); (or whatever the answers to the check are)
and on the site I was working on this:
//————– 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”); }
to this:
//————– BOF Additional Captcha code LJ 31/1/13 2 of 2 ————————-//
if ($captcha==””) {
$messageStack->add(‘contact’, “You must enter the answer to the question”); } elseif (!in_array($captcha, $include_list)) {
$messageStack->add(‘contact’, “Your answer to the question is incorrect”); }
Thanks for this, Christopher.
finally made your updates to the post. thanks again.
Hi Mike,
Great work. I’ve made some edits in the backend validation portion, but have applied it to: http://shop.sign-frame.com/index.php?main_page=contact_us
Seems to work well so far. My edits simply validate all the new stuff before standard zencart validation. If my new validation doesn’t work out. I set error to TRUE, and the && $error==FALSE will stop further execution. It meant I could write more standalone validation code, for future improvements. Thanks again.
Awesome, man. Thanks so much for sharing.