This is how I added a timer to a form to try and filter out fast form-filling by robots....not to be taken as an example of good programming...I write code so I can still understand it the next year/day.
This works for me, no doubt someone will explain why its a bad idea/bad programming but hopefully suggest improvements or a relevant php tutorial.
I did this recently (in addition to a honeypot) for the Ask a Question form as that was getting spammed. As part of my 151 upgrade I am rationalising the antispams across all the forms I have.
The idea is record the server time when the form is generated and subtract it from the time when the form is submitted and if its too fast (you decide), don’t allow it to be processed.
- \includes\templates\YOUR_TEMPLATE\templates\tpl_contact_us_default.php
Add another field in the form
<input type="hidden" name="form_load_time" value="<?php echo $_SERVER['REQUEST_TIME']; //when was the form loaded? ?>" />
- add the processing in the header
\includes\modules\pages\contact_us\header_php.php
This section
if (isset($_GET['action']) && ($_GET['action'] == 'send')) {
handles the form input fields.
Add these lines inside this “if” section:
$form_load_time = (int)zen_db_prepare_input($_POST['form_load_time']);// when was form loaded
$form_submit_time = $_SERVER['REQUEST_TIME'];//when was form submitted
$form_fill_time = $form_submit_time-$form_load_time;//how long did it take to fill in
define (CONTACT_US_MIN_TIME, '5');//seconds..(obviously should be in the admin)
$fast_form_filling = ($form_fill_time < (int)CONTACT_US_MIN_TIME) ? true : false;//true when form was filled in too fast
This should go in a new function if you want to use this across all the forms.
- Now add the check before the form is processed/email is generated, still in
\includes\modules\pages\contact_us\header_php.php
Change
if ($zc_validate_email and !empty($enquiry) and !empty($name) && $error == FALSE)
to
if ($zc_validate_email and !empty($enquiry) and !empty($name) and $fast_form_filling && $error == FALSE)
If the form is submitted in less than 5 seconds the form is just redisplayed. You will need to play around with the value to suit a slow site.
Steve
github.com/torvista: BackupMySQL, Structured Data, Multiple Copy-Move-Delete, Google reCaptcha, Image Checker, Spanish Language Pack and more...