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">

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

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

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

          <label for="services">Services you're interested in:</label>
          <table width="250" border="0" cellspacing="2">
              <td><input type="checkbox" name="commercial" /> Commercial</td>
              <td><input type="checkbox" name="residential" class="threepxfix" /> Residential</td>
              <td><input type="checkbox" name="construction" /> Construction</td>
              <td><input type="checkbox" name="maintenance" class="threepxfix" /> Maintenance</td>
          <br />
          <br />&nbsp;
          <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" />


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" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<META HTTP-EQUIV="refresh" content="0;URL=thankyou.php">
<title>Email Form</title>


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

  $toemail = "";
  $subject = "From";

  $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>\

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

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.