Help creating a spam filter question

Hi everyone,

I hope you can help me as I am not very familiar with PHP. I’m trying to learn as I go but obviously I have a long way to go.

I created a form and want to add a simple spam filter question like “What color is grass?” If the user gets the question wrong, then it will go to an error page. If the question is right, it will go to the thank you page.

Here is the form I have right now:

<form id="formmail" class="cssform" action="formmail.php" method="post">

          <p>
          <label for="name">Name</label>
          <input name="name" type="text" id="name" value="" />
          </p>

          <p>
          <label for="email">Email</label>
          <input name="email" type="text" id="email" value="" />
          </p>

          <p>
          <label for="phone">Phone</label>
          <input name="phone" type="text" id="phone" value="" />
          </p>

          <p>
          <label for="services">Services you're interested in:</label>
          <table width="250" border="0" cellspacing="2">
            <tr>
              <td><input type="checkbox" name="commercial" /> Commercial</td>
              <td><input type="checkbox" name="residential" class="threepxfix" /> Residential</td>
            </tr>
            <tr>
              <td><input type="checkbox" name="construction" /> Construction</td>
              <td><input type="checkbox" name="maintenance" class="threepxfix" /> Maintenance</td>
            </tr>
          </table>
          </p>
          <br />
          <br />&nbsp;
          <p>
          <label for="comment">Comments/Questions:</label>
          <textarea name="comment" id="comment" cols="45" rows="5"></textarea>
          </p><br />

          <div style="margin-left: 150px;">
          <input type="submit" value="Submit" /> <input type="reset" value="Reset" />
          </div>

        </form>

And here is my PHP that I’m using (I didn’t program it myself):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<META HTTP-EQUIV="refresh" content="0;URL=thankyou.php">
<title>Email Form</title>
</head>

<body>
<?php
  $name=addslashes($_POST['name']);
  $phone=addslashes($_POST['phone']);
  $email=addslashes($_POST['email']);
  $commercial=addslashes($_POST['commercial']);
  $residential=addslashes($_POST['residential']);
  $construction=addslashes($_POST['construction']);
  $maintenance=addslashes($_POST['maintenance']);
  $comment=addslashes($_POST['comment']);


 // you can specify which email you want your contact form to be emailed to here

  $toemail = "xxxx@gmail.com";
  $subject = "From XXXX.net";

  $headers = "MIME-Version: 1.0\
"
            ."From: \\"".$name."\\" <".$email.">\
"
            ."Content-type: text/html; charset=iso-8859-1\
";

  $body = "Name: ".$name."<br>\
"
  			."Email: ".$email."<br>\
"
            ."Phone: ".$phone."<br>\
"
			."Commercial: ".$commercial."<br>\
"
			."Residential: ".$residential."<br>\
"
			."Construction: ".$construction."<br>\
"
            ."Maintenance: ".$maintenance."<br>\
"
            ."Comments:<br>\
"
            .$comment;

    mail($toemail, $subject, $body, $headers);
    echo "Thank you for contacting us! We will respond shortly";
?>
</body>
</html>

I did notice that no matter what you entered in the form, it will automatically pull up the thankyou.php but I would like to remove that and have the spam filter redirect based on the answer.

Thank you!

A few things.

First I don’t see where you ask about the color of the grass.
Second, even if you did, you are not doing a comparison to see if they answered correctly.

Also, you may want to sanitize your user’s input. See function filter_var() in the php manual.

and lastly,

<META HTTP-EQUIV=“refresh” content=“0;URL=thankyou.php”>

You are telling the browser to refresh automatically to thankyou.php no matter what. You need to look into the header function to do what you want.

Good luck.

Thanks for the tips. I’m still trying to learn this stuff, which is why I haven’t added anything yet. I was just posting to see if someone could point me in the right direction as to how I could change up the code so I could add the features I need for the spam filter.

I’ll look into the header function and user input.