Simple PHP math captcha to minimize bots sending you junky emails?

Hi,

The way a contact form normally works, is that you submit it to a PHP script, validate the data submitted, then (providing the data is valid) use PHP’s mail function to send a mail to a specified address.

I have a feeling that this wasn’t working or was somehow more complicated on your original host (Yahoo).
Is that correct?

It’s been a long time, so I don’t remember…

If we can just get it to check that a person entered 12 into the math answer space, and send whatever the form data was entered, that will be a good first step.

But if having my email address in the PHP script causes junk messages to begin flooding my Inbox, then we can come up with a Plan B later.

The link you provided about PHP mail function appeared to have some of the right pieces in it. But I need help putting them together…

Ok, well, let’s start with a very basic contact form.
It consists of two files contact.html and myScript.php
You can leave contact.html unchanged, but you’ll have to enter your email address in to myScript.php

Try that out. It should validate, then send send whatever form data was entered.
If it works we can work on the captcha afterwards.

contact.html

<!DOCTYPE HTML>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Contact Form</title>
    <style>
      body{
        font-family: Arial, Helvetica, sans-serif;
      }
      
      .form ul {
        width:500px;
        list-style-type:none;
        list-style-position:outside;
        margin:0px;
        padding:0px;
      }
      
      .form li{
        padding:10px; 
        position:relative;
      }
      
      .form label {
        width:80px;
        margin-top: 2px;
        display:inline-block;
        float:left;
        padding:3px;
      }
      
      .required_notification {
        color: #F06;
        margin:5px 0 0 0; 
        font-size:90%
      }

      h1{
        font-size:160%;
      }
      
      .form input[type="text"]{ 
        font-size: 80%;
        height:20px; 
        width:180px; 
        margin: 0px;
        padding:2px 5px;
        border-radius: 2px;
        -moz-border-radius: 2px;
        -webkit-border-radius: 2px;
      }
      
      .form textarea { 
        width: 240px;
        height: 100px;
        padding: 5px 5px;
        border-radius: 2px;
        -moz-border-radius: 2px;
        -webkit-border-radius: 2px;
      }

    </style>
  </head>
  
  <body>
    <form name="form" id="form" class="form" action="myScript.php" onsubmit="return validate(this)" method="post">
      <ul>
        <li>
          <h1>Contact Us</h1>
          <span class="required_notification">* Denotes Required Field</span>
        </li>
        <li>
          <label for="name">Name:</label>
          <input type="text" name="name" id="name" placeholder="Enter your full name *" />
        </li>
        <li>
          <label for="email">Email:</label>
          <input type="text" name="email" id="email" placeholder="Enter your email address *" />
        </li>
        <li>  
          <label for="message">Message:</label>
          <textarea id="message" name="message" placeholder="Enter your message here...*"></textarea>     
        </li>
        <li>
          <input type="submit" value="submit" class="submit"/>
        </li>
      </ul>
    </form>
  </body>
</html>

myScript.php

<?php
  $name = filter_var($_POST['name'],FILTER_SANITIZE_STRING);
  $email = filter_var($_POST['email'],FILTER_SANITIZE_STRING, FILTER_VALIDATE_EMAIL);
  $message = filter_var($_POST['message'],FILTER_SANITIZE_STRING);
      
  if (empty($name)){
    $error .= "You didn't enter a name <br />";
  }
  
  if (empty($email)){
    $error .= "You didn't enter an email address <br />";
  } elseif (!eregi("^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,})\\.?$", $email)){
    $error .= "The email address appears to be invalid <br />";  
  }
  
  if (empty($message)){
    $error .= "You didn't enter a message <br />";
  }
  
  if (empty($error)) {
    $toaddress = "your address here";
    $fromaddress = "From:". $email;
    
    $mailcontent = "Name: ". $name. "\
".
                   "Email: ". $email. "\
\
".
                   $message;
                   
    mail($toaddress, "Website Contact Form", $mailcontent, $fromaddress);
    header("location: http://www.sitepoint.com");
  } else {
    echo $error;
    echo "<p><strong>Please use your browser's back button and rectify this!</strong></p>";
    exit();
  }
?>