which part can't you get your head around?
generating the captcha (tons of examples)
saving something for comparison upon submittal
testing properly upon submittal
The link I gave is actually REALLY simple code, really easy to use. I'd start with that, and if sessions don't work we can try cookies. embedding the result in the form in this particular case becomes a bit trickier as the image-generator doesn't store the result except in session/cookie (as it runs as part of the html load of the form...).
Here's what I'd do. Use his captcha_image.php file, with one minor modification: change $_SESSION["pass"] to something more like $_SESSION["mailform_captch
a_pass"], to be specific (so it doesn't conflict with 'pass' in some other code in the future...).
Then, modifying your code above to be something like the following (I haven't tested this!):
$submit = $_POST['submit'];
// validate the captcha here:
if ( ($_POST["captcha_input"] == $_SESSION["mailform_captch
put"]) && !empty($_SESSION["mailform
// succeeded, so we go through the rest of the post variables, collect, and email.
$to = "firstname.lastname@example.org";
$name = $_POST['name'];
$name = stripslashes($name);
$phone = $_POST['phone'];
$phone = stripslashes($phone);
$email = $_POST['email'];
$email = stripslashes($email);
$subject = "A message from the website.\n";
$headers = "From: email@example.com
$headers .= "Reply-To: $email\r\n";
$headers .= "Return-Path: firstname.lastname@example.org\r\n";
$body = stripslashes($body);
$message = "Name: $name\nPhone: $phone\nEmail: $email\nDate: " . date("l, F jS, Y @ g:ia") . "\n\nMessage:\n$body\n";
// NOTE: you'd normally ERROR CHECK the call to mail, in case it doesn't succeed...
echo "Your message was sent successfully!\n";
echo "Thank you for the message.\n";
echo "· <a href=\"$PHP_SELF\">Send another message</a><br>\n";
// we're done, exit this script.
// ELSE, we either failed the captcha test, failed to enter a needed field, or this is a new page...
if (isset($submit)) // if got here with a submit, must have failed. Give some useful output?
echo "Please try entering your information again, with the proper code.<br/>";
// html form below, as html for ease of coding.
All fields should be considered <em>required</em>.<br/>
Your Full Name:<br />
<input name="name" type="text" id="name" style="width: 250px; margin-bottom: 10px;" /><br/>
Email Address:<br />
<input name=\"email\" type=\"text\" id=\"email\" style=\"width: 250px; margin-bottom: 10px;\" /><br />
Phone Number:<br />
<input name=\"phone\" type=\"text\" id=\"phone\" style=\"width: 250px; margin-bottom: 10px;\" /><br />
Question or Comment:<br />
<textarea name="body" wrap="VIRTUAL" id="query" style="width: 250px; height: 100px; margin-bottom: 10px;" /></textarea><br/>
Please enter the text you see in the following graphic:<br/>
<img src="captcha_image.php" />: <input type="text" name="captcha_input" size="15"><br/>
<input type="submit" name="submit" id="submit" value="Send It"> <input type="reset" name="reset" id="reset" value="Reset">
<?php /* end of form */ ?>