Simple PHP Anti-Spam and Form Validation Tutorial

Everyone is sick and tired of receiving spam through web forms – how do we stop it?
There are several ways to stop spammers, including the use of Captcha.

In this tutorial, we will be creating a simple form-to-email script with field validation and simple anti-apam protection using PHP.

The script will take the entered values from a field called “name” and a text area called “message”, and email them through to a chosen email address (which we will set).

Lets begin…

1) Create a simple HTML form.
We will start by creating a form (called “form”) that will post to a PHP file called “post_form.php” (which we will create later on).

1
2
3
<form id="form" name="form" method="post" action="post_form.php">
 
</form>

On our form we simply want to send our name and a brief message.
So we add our “name” field, “message” text area, and the “submit” button.

<form id="form" name="form" method="post" action="post_form.php">
 
<p>Name<br />
<input type="text" name="name" id="name" /></p>
 
<p>Message<br />
<textarea name="message" id="message" cols="45" rows="5"></textarea></p>
 
<p><input type="submit" name="button" id="button" value="Submit" /></p>
 
</form>

Now we have a simple form that will post the values of “name” and “message” through to a file called “post_form.php”.

Moving on…

2) Putting the anti-spam code onto the form.
Ok, so now we start to add a bit of PHP, and a couple of fields.
We will add an anti-spam section showing the randomised 4 digit code, 1 hidden field containing the code called “kiwi”, and 1 field for the user to enter the code, called “apple”.

First of all we create a variable called “$rand” and give it a random value between 1000 and 9999 using the PHP rand() command.
We then put the value of “$rand” into a hidden field called “kiwi”.
Finally, we add add a field called “apple” to the form, which is the field where the user will have to enter the code into.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<form id="form" name="form" method="post" action="post_form.php">
 
<p>Name<br />
<input type="text" name="name" id="name" /></p>
 
<p>Message<br />
<textarea name="message" id="message" cols="45" rows="5"></textarea></p>
 
<p>Anti-Spam<br />
Please enter this code 
<?PHP 
	$rand = rand(1000,9999);
	echo "<input type=\"hidden\" name=\"kiwi\" id=\"kiwi\" value=\"$rand\"/>";
        echo "<strong>".$rand."</strong> ";
?>
into this field <input name="apple" type="text" id="apple" size="4" maxlength="4" /></p>
 
<p><input type="submit" name="button" id="button" value="Submit" /></p>
 
</form>

Save file as “index.php”;

3) Creating post_form.php
In this final section, I will show you how to validate the anti-Spam code that has been entered by the user, make sure that there is text in the “name” and “message” fields, and also show you how to mail the message.

Start by adding an if() statement to check if the file has been posted to.

1
2
3
4
5
6
7
<?PHP
 
if($_POST){
 
} // END IF POST
 
?>

Next, we will bring in the two anti-Spam fields “kiwi” and “apple”, and compare the two to see if they match using a simple if() statement; if “kiwi” doesn’t equal “apple” then it will show the error message “The Anti-Spam code is incorrect. Please go back and try again.”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?PHP
 
if($_POST){
 
	$kiwi = $_POST['kiwi'];
	$apple = $_POST['apple'];
 
	if($kiwi != $apple){
 
		echo "<ul>
		          <li>The Anti-Spam code is incorrect. Please go back and try again.</li>
		      </ul>";
 
	}else{
 
	} // END ANTI-SPAM CHECK
 
} // END IF POST
 
?>

We now also bring in the “name” and “message” fields, making sure that they both contain some text using if() statements.

Starting off with checking if both fields are blank, show the error message “No name entered. Please go back and enter a name.” and “No message entered. Please go back and enter a message.”

Then if only the “name” field is empty, show error message “No name entered. Please go back and enter a name.”

And finally, if only the “message” field is empty, show error message “No message entered. Please go back and enter a message.”

<?PHP
 
if($_POST){
 
	$kiwi = $_POST['kiwi'];
	$apple = $_POST['apple'];
 
	if($kiwi != $apple){
 
		echo "<ul>
		          <li>The Anti-Spam code is incorrect. Please go back and try again.</li>
		      </ul>";
 
	}else{
                $name = $_POST['name'];
		$message = $_POST['message'];
 
		if($name == '' && $message == ''){
 
			echo "<ul>
				   <li>No name entered. Please go back and enter a name.</li>
			           <li>No message entered. Please go back and enter a message.</li>
			      </ul>";
 
		}else if($name == ''){
 
			echo "<ul>
				  <li>No name entered. Please go back and enter a name.</li>
			      </ul>";
 
		}else if($message == ''){
 
			echo "<ul>
			          <li>No message entered. Please go back and enter a message.</li>
			      </ul>";
 
		}
 
	} // END ANTI-SPAM CHECK
 
} // END IF POST
 
?>

Ok, so the last part of our “post_form.php” script, the mailing part.

We set the “to” variable with the email address we would like the message to be sent to.
The “from” variable, which we will set to an email address at your domain.
Setting the “subject” field to the subject of the email, in this case “This is a test”.
And the “mail_message” variable, that contains the “name” posted value and the “message” posted value.

If all goes well, you should then also receive a message “$name, your message has been sent successfully”.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?PHP
 
if($_POST){
 
	$kiwi = $_POST['kiwi'];
	$apple = $_POST['apple'];
 
	if($kiwi != $apple){
 
		echo "<ul>
			  	<li>The Anti-Spam code is incorrect. Please go back and try again.</li>
			  </ul>";
 
	}else{
 
		$name = $_POST['name'];
		$message = $_POST['message'];
 
		if($name == '' && $message == ''){
 
			echo "<ul>
					<li>No name entered. Please go back and enter a name.</li>
					<li>No message entered. Please go back and enter a message.</li>
				  </ul>";
 
		}else if($name == ''){
 
			echo "<ul>
					<li>No name entered. Please go back and enter a name.</li>
				  </ul>";
 
		}else if($message == ''){
 
			echo "<ul>
					<li>No message entered. Please go back and enter a message.</li>
				  </ul>";
 
		}else{
 
			$to = "test@test.com"; // REPLACE WITH YOUR CHOSEN EMAIL ADDRESS
			$from = "From: form@test.com"; // REPLACE WITH YOUR OWN @ DOMAIN
			$subject = "This is a test"; // THE SUBJECT OF THE EMAIL
			$mail_message = $name." has sent you a message.\n".$message;
 
			mail($to,$subject,$mail_message,$from);
 
			echo "$name, your message has been sent successfully";
 
		}// END IF FIELDS EMPTY
 
	} // END ANTI-SPAM CHECK
 
} // END IF POST
 
?>

Save as “post_form.php”.

If you have any questions, please feel free to message me on Twitter @AnthonyHallMe , or alternatively leave a comment.