HowToPlaza.com

How to wisdom from across the Internet — want to know how to do something? You may find the solution here.


How To Stop Contact Form Spam

Are you being flooded with tons of contact from spam? The spammers use the vulnerability in the PHP script that you create to send you a deluge of messages using some program. This program automatically inserts values in the "header" parameter of the mail() function of PHP to send you contact form spam. I’ve been scouring the Internet to find a nice, small solution. I even tried to use the CAPTCHA image validation but I don’t know why somehow it didn’t work on my contract form. Then I found a simple solution. When you create your form just add an extra field and in that field asked your visitor to enter some information that only a human visitor will be able to enter. For instance create a form like this one:

<form name="cform" id="cform" method="post" action="sendform.php">
<label>Name:</label><br />
<input type="text" name="name" id="name" size="20" /><br />
<label>Email:</label><br />
<input type="text" name="email" id="email" size="20" /><br />
<label>Message:</label><br />
<textarea name="message" id="message" rows="5" cols="30" wrap="soft"></textarea><br />
<label>What’s the capital of China?</label><br />
<input type="text" name="q" id="q" size="20" /><br />
<input type="submit" name="s" value="Submit" />
</form>

In the last field I have asked "What is the capital of China?" and this is going to stop the spamming robot because it wouldn’t be programmed to answer that question. Now let us see what we are going to put in sendform.php.

<?php
$from=$_POST['name'];
$email=$_POST['email'];
$message=$_POST['message'];
$answer=$_POST['q'];
$headers.="From: ‘" . $name . "’ <" . $email . ">\n";
$headers.="Reply-To: ‘" . $name . "’ <" . $email . ">\n";
$toemail="youremail@yourdomain.com";
$subject="Message from website";
If($answer=="Beijing")
{
$show_message="Thank you for leaving comment!";
mail($toemail, $subject, $message, $headers);
}
else
{
$show_message="Please enter the answer for \"What’s the capital of China?\" because I need to verify that you are a human and not a spamming robot.";
}
print $show_message;
?>

This will make sure that the spamming robots are not able to use your contact form to send you spam.

AddThis Social Bookmark Button

Posted by admin | Tags: Uncategorized


You can leave a response, or trackback from your own site.

Leave a Reply