Check box or maths puzzle to combat SPAM

Im facing spam submission on my form here http://share-ask.com .Im unable to PUT Check box or maths puzzle IN my form to combat SPAM .can you help me

What do you mean by “unable”, @Atik1? Do you mean you don’t know how to do it, or do you mean you’ve tried to do it but it is not working as expected?

You should know by now that you need to give full details of your issue in order to receive any useful replies.

Ya i don’t know

You don’t know how to do it? Did you try searching for instructions? There seems to be a good range of tutorials on the subject. For example:

or

Tnx but im looking for hardcoded option

You mean you’re looking for someone to do it for you?

i made dis by online help but ,checkbox option im not able to do after trying ,so can u help

As I said in my first reply to you:

You’re the one looking for assistance, so the onus is upon you to explain clearly what it is that you are asking, and what help you want.

I’m sure I’m not the only member getting very tired of trying to guess what you’re asking, only to have suggestions dismissed because you have requirements you have failed to mention.

If you post the code you tried, someone may be able to see where you went wrong and help correct it.

1 Like

I added below code in a template but validation is not triggerring

<div id="respond">
                <?php echo $response; ?>
                <form action="<?php the_permalink(); ?>" method="post">
                  <p><label for="name">Name: <span>*</span> <br><input type="text" name="message_name" value="<?php echo esc_attr($_POST['message_name']); ?>"></label></p>
                  <p><label for="message_email">Email: <span>*</span> <br><input type="text" name="message_email" value="<?php echo esc_attr($_POST['message_email']); ?>"></label></p>
                  <p><label for="message_text">Message: <span>*</span> <br><textarea rows="15" cols="60" type="text" name="message_text"><?php echo esc_textarea($_POST

['message_text']); ?></textarea></label></p>
                  <p><label for="message_human">Human Verification: <span>*</span> <br><input type="text" style="width: 60px;" name="message_human"> + 3 = 5</label></p>
                  <input type="hidden" name="submitted" value="1">
                  <p><input type="submit"></p>
                </form>
              </div>





							<?php wp_link_pages(array('before' => '<div class="pagination">', 'after' => '</div>', 'link_before'  => '<span class="current"><span class="currenttext">', 'link_after' => '</span></span>', 'next_or_number' => 'next_and_number', 'nextpagelink' => __('Next', 'schema' ), 'previouspagelink' => __('Previous', 'schema' ), 'pagelink' => '%','echo' => 1 )); ?>


							
							<?php if (!empty($mts_options['mts_social_buttons_on_pages']) && isset($mts_options['mts_social_button_position']) && $mts_options['mts_social_button_position'] !== 'top') mts_social_buttons(); ?>
						</div><!--.post-content box mark-links-->
					</div>
				</div>

					
<?php comments_template( '', true ); ?>
			<?php endwhile; ?>
		</div>
	</article>
	<?php get_sidebar(); ?>
<?php get_footer(); ?>


<?php

  //response generation function

  $response = "";

  //function to generate response
  function my_contact_form_generate_response($type, $message){

    global $response;

    if($type == "success") $response = "<div class='success'>{$message}</div>";
    else $response = "<div class='error'>{$message}</div>";

  }

  //response messages
  $not_human       = "Human verification incorrect.";
  $missing_content = "Please supply all information.";
  $email_invalid   = "Email Address Invalid.";
  $message_unsent  = "Message was not sent. Try Again.";
  $message_sent    = "Thanks! Your message has been sent.";

  //user posted variables
  $name = $_POST['message_name'];
  $email = $_POST['message_email'];
  $message = $_POST['message_text'];
  $human = $_POST['message_human'];

  //php mailer variables
  $to = get_option('admin_email');
  $subject = "Someone sent a message from ".get_bloginfo('name');
  $headers = 'From: '. $email . "\r\n" .
    'Reply-To: ' . $email . "\r\n";

  if(!$human == 0){
    if($human != 2) my_contact_form_generate_response("error", $not_human); //not human!
    else {

      //validate email
      if(!filter_var($email, FILTER_VALIDATE_EMAIL))
        my_contact_form_generate_response("error", $email_invalid);
      else //email is valid
      {
        //validate presence of name and message
        if(empty($name) || empty($message)){
          my_contact_form_generate_response("error", $missing_content);
        }
        else //ready to go!
        {
          $sent = wp_mail($to, $subject, strip_tags($message), $headers);
          if($sent) my_contact_form_generate_response("success", $message_sent); //message sent!
          else my_contact_form_generate_response("error", $message_unsent); //message wasn't sent
        }
      }
    }
  }
  else if ($_POST['submitted']) my_contact_form_generate_response("error", $missing_content);

?>
1 Like

This is the processing part of your form with some changes.

<?php

  //response generation function

  $response = "";

  //function to generate response
  function my_contact_form_generate_response($type, $message){

    //global $response;

    if($type == "success") $response = "<div class='success'>{$message}</div>";
    else $response = "<div class='error'>{$message}</div>";
		
		return $response ; // add return, the function must return something
  }

  //response messages
  $not_human       = "Human verification incorrect.";
  $missing_content = "Please supply all information.";
  $email_invalid   = "Email Address Invalid.";
  $message_unsent  = "Message was not sent. Try Again.";
  $message_sent    = "Thanks! Your message has been sent.";

  if($_SERVER['REQUEST_METHOD'] == 'POST'){ // add this important test
  
		//user posted variables
		$name = $_POST['message_name'];
		$email = $_POST['message_email'];
		$message = $_POST['message_text'];
		$human = $_POST['message_human'];
	      
		//php mailer variables
		$to = get_option('admin_email');
		$subject = "Someone sent a message from Outer Space";
		$headers = 'From: '. $email . "\r\n" .
		  'Reply-To: ' . $email . "\r\n";
	      
		if($human != ''){ // condition changed to check empty string or not
		  if($human != 2) { $response = my_contact_form_generate_response("error", $not_human); } // add brackets //not human!  // $response =
		  else {
	      
		    //validate email
		    if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
		      $response =  my_contact_form_generate_response("error", $email_invalid);	 // add brackets  // $response =
		    }
		    else //email is valid
		    {
		      //validate presence of name and message
		      if(empty($name) || empty($message)){
			$response =  my_contact_form_generate_response("error", $missing_content);	 // $resp =
		      }
		      else //ready to go!
		      {
	
			$sent = mail($to, $subject, strip_tags($message), $headers);
			if($sent) { $response =  my_contact_form_generate_response("success", $message_sent); } //message sent! , add brackets  // $response =
			else { $response =  my_contact_form_generate_response("error", $message_unsent); } //message wasn't sent, add brackets  // $response =
		      }
		    }
		  }
		}
		elseif ($_POST['submitted']) {$response =  my_contact_form_generate_response("error", $missing_content); } // elseif is one word, add brackets  // $response =
		
  } // end if POST
// move to top of page ^^
?>

The first thing to note is that this whole block of code should be moved to the beginning of the document, before any html.

There was a problem with your response function, it did not return anything.
Also when you called the function, you did not assign the result to the variable, though there was no result, being no return. In many cases you also had no brackets following the conditions before calling the function.

The form processor did not have any test to determine if any data had been posted. I added this condition for that:-

if($_SERVER['REQUEST_METHOD'] == 'POST'){...}

So the script only runs after a submission.

You were using this condition to check if the captcha had been filled in:-

if(!$human == 0){...}

I changed it for this to check for empty strings only:-

if($human != ''){ ...}

You may have used empty in the condition, but that would pick '0' as being empty.

It may not be the perfect form validation script, but at least it should be functional now.

2 Likes

Thanks ton @SamA74 ,its working now

is it possible for you to add file upload option were form submits file uploaded without storing in dbase

Another method that you might want to also consider is the time taken to submit a form. When the user goes to the page with the form, get a timestamp and store it in the session data. Then when the form is submitted get the current timestamp and calculate the interval between the two timestamps. If the interval is less then a certain time, reject the submitted data and return a generic “Oops something went wrong” error message

3 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.