Today I am going to show you how to create a simple contact form spam checker using php. Some people aren’t keen on image captchas because they think they look out of place, ugly and so on. If this is the case, read on because I will show you how to code an alternative.
Step 1
Assuming you already have a contact form process, lets start by defining the following variables.
$num1 = (rand(1,20)); $num2 = (rand(1,20)); $sum = $num1 + $num2;
What this basically does is generate 2 random numbers using the the rand() function. I also passed in two integers, 1 and 20. This is because I want the random number to be in the range of 1 and 20. The $sum variable is the result of the two random numbers added together.
Step 2
The next stage is to create a security question. Below is an example of the contact form I’ve used.
<form method='post' action='<?php echo $_SERVER['PHP_SELF']; ?>'> <p>To prevent spam, what is the result of <?php echo $num1; ?>+<?php echo $num2; ?>?</p> <input type='text' id='usernum' name='usernum'> <input name='sum' id='sum' type='hidden' value='<?php echo $sum; ?>'/> <input type='submit' value='Submit' /> </form>
As you can see from the above, I have asked the question ‘To prevent spam, what is the result of random number 1 + random number 2′. The correct answer ‘$sum’ is stored in a hidden text field. This data will be sent once the user enters an answer and clicks submit.
Step 3
Once the form has been submitted, I need to get that information by using $_REQUEST and assign the values to the below variables.
$usernum = $_REQUEST['usernum']; $sum = $_REQUEST['sum'];
Step 4
Next I need to check if the users answer is correct. To do this I have used a conditional statement.
if ($usernum !== $sum) { echo '<p>The Answer You Entered Was Wrong</p>'; } else { // Email code goes here.. echo '<p>Your email has been sent </p>'; }
Step 5
The final stage is to wrap the whole lot in a conditional statement to determine whether the user has clicked submit or not.
Final code:
<?php $num1 = (rand(1,20)); $num2 = (rand(1,20)); $sum = $num1 + $num2; if (isset($_REQUEST['usernum'])) : $usernum = $_REQUEST['usernum']; $sum = $_REQUEST['sum']; if ($usernum !== $sum) { echo '<p>The Answer You Entered Was Wrong</p>'; } else { // Email code goes here.. echo '<p>Your email has been sent </p>'; } else : ?> <form method='post' action='<?php echo $_SERVER['PHP_SELF']; ?>'> <p>To prevent spam, What is the result of <?php echo $num1; ?>+<?php echo $num2; ?>?</p> <input type='text' id='usernum' name='usernum'> <input name='sum' id='sum' type='hidden' value='<?php echo $sum; ?>'/> <input type='submit' value='Submit' /> </form> <?php endif; ?>
I hope you found this web design tutorial useful.
07:39 06/08/2011
Thanks for sharing this information.. it will be of great help !
Website Designers
14:39 30/08/2011
Short, simple and sweet.
I would suggest changing the values to:
Why make the user work too hard?
I would also suggest making a AJAX version for loading incorrect answers in text below the form. I hope that helps!
08:17 19/09/2011
Thanks for your comment Joe. Good point about the numbers range. I’m thinking of creating an jQuery contact form soon which uses the AJAX methods – I will post it on here once done.