title: How to validate the captcha
body: <p>I am using a custom php captcha on a website and I am unable to get the php that send the email to check if the captcha was completed successfully. Here is the code:</p>
  
  <p>Form: </p>
  
  <pre><code>&lt;form method="POST" name="contact_form" action="/templates/onlinespark/contact-form-handler.php"&gt; 
      &lt;label for="name"&gt;Name: &lt;/label&gt;
      &lt;input type="text" name="name" value="&lt;?php echo htmlentities($name); ?&gt;"&gt;
      &lt;label for='email'&gt;Email: &lt;/label&gt;
      &lt;input type="text" name="email" value="&lt;?php echo htmlentities($visitor_email); ?&gt;"&gt;
      &lt;label for="phone"&gt;Phone: &lt;/label&gt;
      &lt;input type="text" name="phone" value='&lt;?php echo htmlentities($phone); ?&gt;'&gt;
      &lt;label for="message"&gt;Message:&lt;/label&gt;
      &lt;textarea name="message" rows="8" cols="30"&gt;&lt;?php echo htmlentities($user_message); ?&gt;&lt;/textarea&gt;
      &lt;label&gt;&lt;img src="/templates/onlinespark/captcha.php"&gt;&lt;/label&gt;
      &lt;input type="text" name="code"&gt; 
      &lt;input type="submit" value="Submit" name="submit" class="quoteButton"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>PHP: contact-form-hander.php</p>
  
  <pre><code>&lt;?php 
      if (isset($_POST['submit'])) {
          $error = "";
          if (!empty($_POST['name'])) {
               $name = $_POST['name'];
          } else {
               $error .= "You didn't type in your name. &lt;br /&gt;";
          }
  
          if (!empty($_POST['phone'])) {
              $name = $_POST['phone'];
          } else {
              $error .= "You didn't enter your phone. &lt;br /&gt;";
          }
  
          if (!empty($_POST['email'])) {
              $email = $_POST['email'];
              if (!preg_match("/^[a-z0-9]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)){ 
                  $error .= "The e-mail address you entered is not valid. &lt;br/&gt;";
              }
           } else {
              $error .= "You didn't type in an e-mail address. &lt;br /&gt;";
           }
  
           if (!empty($_POST['message'])) {
               $message = $_POST['message'];
           } else {
               $error .= "You didn't type in a message. &lt;br /&gt;";
           }
  
           if(($_POST['code']) == $_SESSION['code']) { 
               $code = $_POST['code'];
           } else { 
               $error .= "The captcha code you entered does not match. Please try again. &lt;br /&gt;";    
           }
  
           if (empty($error)) {
               $from = 'From: ' . $name . ' &lt;' . $email . '&gt;';
               $to = "mail@email.com";
               $subject = "New contact form message";
               $content = $name . " has sent you a message: \n" . $message;
               $success = "&lt;h3&gt;Thank you! Your message has been sent!&lt;/h3&gt;";
               mail($to,$subject,$content,$from);
           }
      }
  ?&gt;
  &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt; 
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;ERROR - Please fill in all fields!&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;!-- This page is displayed only if there is some error --&gt;
  &lt;h1&gt;ERROR - Please go back and fill in all fields!&lt;/h1&gt;
  &lt;?php
      if (!empty($error)) {
          echo '&lt;p class="error"&gt;&lt;strong&gt;Your message was NOT sent&lt;br/&gt; The following error(s) returned:&lt;/strong&gt;&lt;br/&gt;' . $error . '&lt;/p&gt;';
      } elseif (!empty($success)) {
          echo $success;
      }
  ?&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Basically I need the external php file that sends the mail to check to see if the captcha was completed correctly before it sends the mail. At the moment it seems to be ignoring the captcha all together.  What do I need to do?</p>
  
  <p>Thanks!</p>