Preventing Webforms Submissions from bots in your Drupal site

Tired of incessant spam webform submissions from bots?

All have unique quality : they contain links of sites they are promoting or may contain junk links - links of sites which don't exist.

This module( Drupal 6)actually consists of two things:

  • Prevent anyone from inserting links in the body. You can configure $MAX_LINKS_ALLOWED value yourself. If it's value is 0 then it means no links will be allowed.
  • Prevent anyone from inserting even single link in the comment form. Also the "Home page" field will be disabled from view so that there is no scope of entering links through comment form.

Take care to name the webform(Edit->Form Components->Field Key) textarea component to receive the comments to be named as 'comments_questions_feedback', if it is not then replace this text in the code below to the name you've used.

Just create a folder name "anu_disable_http" in site/all/modules and place these two files and then enable them in your modules( path: admin/build/modules)

anu_disable_http.module

<?php

function anu_disable_http_form_alter(&$form, &$form_state, $form_id) {
   
    global
$user;

    if (
$user->uid == 1) {
        return;
    }

    if (
$form_id == 'comment_form') {
       
$form['#validate'][] = 'anu_disable_http_validate_comments';

        if (isset(
$form['homepage'])) {
           
$form['homepage']['#access'] = FALSE; //disable homepage
       
}
    } else if (
preg_match('/^webform_client_form_[0-9]+/', $form_id)) {

       
$form['#validate'][] = 'anu_disable_http_validate_webform_comments';
    } else {
       
    }
}

function
anu_disable_http_validate_comments($form, $form_state) {
   
$pattern = '/\bhttps?:\/\//i';
    if (
preg_match($pattern, $form_state['values']['comment'])) {
       
form_set_error('comment', "Please remove http:// or https:// from links, we will add them if required");
    }
}

function
anu_disable_http_validate_webform_comments($form, $form_state) {
   
$MAX_LINKS_ALLOWED = 1;
   
$val = $form_state['values']['submitted']['comments_questions_feedback'];

   
$pattern = '/\bhttps?:\/\//is';
   
//see if more than one http is found then disallow
   
if (preg_match_all($pattern, $val) > $MAX_LINKS_ALLOWED) {
       
form_set_error('submitted][comments_questions_feedback', "Please remove http:// or https:// from links, we will add them if required");
    }
}

?>

anu_disable_http.info

; $Id: anu_disable_http.info,v 1.1 2012/09/26 09:08:13 D14 Exp $
name = Anu Disable http in comments
description = to prevent spam
files[] = anu_disable_http.module
core = 6.x
package = custom
; Information added by drupal.org packaging script on 2010-11-12
version = 1.0
core = 6.x