I have form on my companies website that isn't being attacked my spammers. I am looking for a simple piece of code to put in the form above the submit button. Something simple like "1+2= ..." then they have to put in 3, or the form rejects. If the put in the correct answer, then the form submits. How can I do this?
Depends.... 1) are you wanting the "test" to be static or dynamic (ie is it always 1 + 2 or is it RandomNumber1 RandomMathsFunction RandomNumber2 = ...?) 2) What serverside language are you using? PHP? .Net?
It's usually done by generating 2 random numbers (usually between 1 and 20) and then adding them together. Then, storing a hash of the answer in a session. When the form is submitted, check the form input (hashed) against the session variable.
$questions[] = array('question','answer'); $questions[] = array('question','answer'); $questions[] = array('question','answer'); //form start $x = rand(0,count($questions)-1); echo $questions[$x][0]; echo '<input type="text" id="ans" name="ans" value="">'; echo '<input type="hidden" id="q" name="q" value="$x">'; //form end //In your submit validation $pos = $_POST['q']; if($_POST['ans'] != $questions[$pos][1]){ die('Invalid answer');} PHP: Peace,