Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Sep 2009
    Posts
    53
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Need help adding a CAPTCHA code to a php e-mail form.

    Hi,

    I have a few forms already working but spam protection has become a bigger concern so i was hoping someone would be able to show me the code i need to insert to this already functioning code to allow it to have a CAPTCHA code at the end of it. Thanks in advance.

    this is the html area that calls the php file.

    Code:
    <div id="bodytext2">
    <form name="contactform" method="post" action="send_form_email.php">
      <table width="604" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td><label for="first_name" class="bodytext">
          <div align="left" class="bodytext">First Name *</div>
          </label></td>
          <td><label for="last_name" class="bodytext">
          <div align="left" class="bodytext">Last Name *</div>
          </label></td>
        </tr>
        <tr>
          <td height="32"><div align="left">
            <input  type="text" name="first_name" maxlength="50" size="30" />
          </div></td>
          <td><div align="left">
            <input  type="text" name="last_name" maxlength="50" size="30" />
          </div></td>
        </tr>
        <tr>
          <td><label for="email" class="bodytext">
          <div align="left" class="bodytext">Email Address *</div>
          </label></td>
          <td><label for="telephone" class="bodytext">
          <div align="left" class="bodytext">Telephone Number *</div>
          </label></td>
        </tr>
        <tr>
          <td><div align="left">
            <input  type="text" name="email" maxlength="80" size="30" />
          </div></td>
          <td><div align="left">
            <input  type="text" name="telephone" maxlength="10" size="30" />
          </div></td>
        </tr>
      </table><br />
      <table width="604" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td><label for="comments" class="bodytext">
        <div align="left" class="bodytext">Comments *</div>
        </label></td>
      </tr>
      <tr>
        <td><div align="left">
          <textarea  name="comments" cols="62" rows="10">Please write your comments here...</textarea>
        </div></td>
      </tr>
        <tr>
    <td colspan="2" style="text-align:center"><div align="left">
      <input type="submit" value="Submit" />
    </div>
    </tr>
    </table>
    </form>
    
    
    
    </div>
    This is the PHP area that it is calling:

    Code:
    <?php
    if(isset($_POST['email'])) {
    	
    	// EDIT THE 2 LINES BELOW AS REQUIRED
    	$email_to = "example@example.com";
    	$email_subject = "example";
    	
    	
    	function died($error) {
    		// your error code can go here
    		echo "We are very sorry, but there were error(s) found with the form your submitted. ";
    		echo "These errors appear below.<br /><br />";
    		echo $error."<br /><br />";
    		echo "Please go back and fix these errors.<br /><br />";
    		die();
    	}
    	
    	// validation expected data exists
    	if(!isset($_POST['first_name']) ||
    		!isset($_POST['last_name']) ||
    		!isset($_POST['email']) ||
    		!isset($_POST['telephone']) ||
    		!isset($_POST['comments'])) {
    		died('We are sorry, but there appears to be a problem with the form your submitted.');		
    	}
    	
    	$first_name = $_POST['first_name']; // required
    	$last_name = $_POST['last_name']; // required
    	$email_from = $_POST['email']; // required
    	$telephone = $_POST['telephone']; // not required
    	$comments = $_POST['comments']; // required
    	
    	$error_message = "";
    	$email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
      if(!eregi($email_exp,$email_from)) {
      	$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
      }
    	$string_exp = "^[a-z .'-]+$";
      if(!eregi($string_exp,$first_name)) {
      	$error_message .= 'The First Name you entered does not appear to be valid.<br />';
      }
      if(!eregi($string_exp,$last_name)) {
      	$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
      }
      if(strlen($comments) < 2) {
      	$error_message .= 'The Comments you entered do not appear to be valid.<br />';
      }
      $string_exp = "^[0-9 .-]+$";
      if(!eregi($string_exp,$telephone)) {
      	$error_message .= 'The Telphone Number you entered does not appear to be valid.<br />';
      }
      if(strlen($error_message) > 0) {
      	died($error_message);
      }
    	$email_message = "Form details below.\n\n";
    	
    	function clean_string($string) {
    	  $bad = array("content-type","bcc:","to:","cc:","href");
    	  return str_replace($bad,"",$string);
    	}
    	
    	$email_message .= "First Name: ".clean_string($first_name)."\n";
    	$email_message .= "Last Name: ".clean_string($last_name)."\n";
    	$email_message .= "Email: ".clean_string($email_from)."\n";
    	$email_message .= "Telephone: ".clean_string($telephone)."\n";
    	$email_message .= "Comments: ".clean_string($comments)."\n";
    	
    	
    // create email headers
    $headers = 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers);  
    ?>
    
    <!-- include your own success html here -->
    <span class="bodytext">
     Thank you for contacting us. We will be in touch with you very soon.</span>
    <?
    }
    ?>

    I'm a bit new to PHP so if your responses could be as user friendly as possible it would be greatly appreciated.

    Thanks again

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts

  • #3
    New Coder
    Join Date
    Mar 2008
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts
    I recently switched to recaptcha and it was easy to install. Try reading up on it at recaptcha.net. You can install it using all PHP or javascript and PHP or a variety of other formats.
    Last edited by wilcosky; 09-19-2009 at 04:56 AM. Reason: misspelled something

  • #4
    New Coder
    Join Date
    May 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I second reCAPTCHA, easy to install and works great.

  • #5
    New Coder
    Join Date
    Sep 2009
    Posts
    53
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by angst View Post
    I went with php captcha and installed it easily within the html end, but the php end is where i'm having trouble.

    I have no idea where to enter the code their suggesting. Well I thought i had an idea, but it was the wrong one.


    Here is the HTML area of the form which is working correctly
    Code:
    <div id="bodytext2">
    <form name="contactform" method="post" action="send_form_email.php">
      <table width="604" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td><label for="first_name" class="bodytext">
          <div align="left" class="bodytext">First Name *</div>
          </label></td>
          <td><label for="last_name" class="bodytext">
          <div align="left" class="bodytext">Last Name *</div>
          </label></td>
        </tr>
        <tr>
          <td height="32"><div align="left">
            <input  type="text" name="first_name" maxlength="50" size="30" />
          </div></td>
          <td><div align="left">
            <input  type="text" name="last_name" maxlength="50" size="30" />
          </div></td>
        </tr>
        <tr>
          <td><label for="email" class="bodytext">
          <div align="left" class="bodytext">Email Address *</div>
          </label></td>
          <td><label for="telephone" class="bodytext">
          <div align="left" class="bodytext">Telephone Number *</div>
          </label></td>
        </tr>
        <tr>
          <td><div align="left">
            <input  type="text" name="email" maxlength="80" size="30" />
          </div></td>
          <td><div align="left">
            <input  type="text" name="telephone" maxlength="10" size="30" />
          </div></td>
        </tr>
      </table><br />
      <table width="604" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td><label for="comments" class="bodytext">
        <div align="left" class="bodytext">Comments *</div>
        </label></td>
      </tr>
      <tr>
        <td><div align="left">
          <textarea  name="comments" cols="62" rows="10">Please write your comments here...</textarea>
        </div></td>
      </tr>
        <tr>
    <td colspan="2" style="text-align:center"><div align="left">
    <img id="captcha" src="/securimage/securimage_show.php" alt="CAPTCHA Image" />
    <input type="text" name="captcha_code" size="10" maxlength="6" />
    <a href="#" class="ex2" onclick="document.getElementById('captcha').src = '/securimage/securimage_show.php?' + Math.random(); return false">Reload Image</a>
    
    </div></tr>
        <tr>
          <td colspan="2" style="text-align:center"><div align="right">
            <input type="submit" value="Submit" />    
          </div>
        </tr>
      </table>
    </form>
    </div>

    This is the php area:
    Code:
    <?php
    if(isset($_POST['email'])) {
    	
    	// EDIT THE 2 LINES BELOW AS REQUIRED
    	$email_to = "blank@blank.com";
    	$email_subject = "blah balh";
    	
    	
    	function died($error) {
    		// your error code can go here
    		echo "We are very sorry, but there were error(s) found with the form your submitted. ";
    		echo "These errors appear below.<br /><br />";
    		echo $error."<br /><br />";
    		echo "Please go back and fix these errors.<br /><br />";
    		die();
    	}
    	
    	// validation expected data exists
    	if(!isset($_POST['first_name']) ||
    		!isset($_POST['last_name']) ||
    		!isset($_POST['email']) ||
    		!isset($_POST['telephone']) ||
    		!isset($_POST['comments'])) {
    		died('We are sorry, but there appears to be a problem with the form your submitted.');		
    	}
    	
    	
    	$first_name = $_POST['first_name']; // required
    	$last_name = $_POST['last_name']; // required
    	$email_from = $_POST['email']; // required
    	$telephone = $_POST['telephone']; // not required
    	$comments = $_POST['comments']; // required
    	
    	$error_message = "";
    	$email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
      if(!eregi($email_exp,$email_from)) {
      	$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
      }
    	$string_exp = "^[a-z .'-]+$";
      if(!eregi($string_exp,$first_name)) {
      	$error_message .= 'The First Name you entered does not appear to be valid.<br />';
      }
      if(!eregi($string_exp,$last_name)) {
      	$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
      }
      if(strlen($comments) < 2) {
      	$error_message .= 'The Comments you entered do not appear to be valid.<br />';
    
    
    	}
    
      $string_exp = "^[0-9 .-]+$";
      if(!eregi($string_exp,$telephone)) {
      	$error_message .= 'The Telephone Number you entered does not appear to be valid.<br />';
      }
      if(strlen($error_message) > 0) {
      	died($error_message);
      }
    	$email_message = "Form details below.\n\n";
    	
    	function clean_string($string) {
    	  $bad = array("content-type","bcc:","to:","cc:","href");
    	  return str_replace($bad,"",$string);
    	}
    	
    	$email_message .= "First Name: ".clean_string($first_name)."\n";
    	$email_message .= "Last Name: ".clean_string($last_name)."\n";
    	$email_message .= "Email: ".clean_string($email_from)."\n";
    	$email_message .= "Telephone: ".clean_string($telephone)."\n";
    	$email_message .= "Comments: ".clean_string($comments)."\n";
    	
    	
    // create email headers
    $headers = 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers);  
    ?>
    
    <!-- include your own success html here -->
    <span class="bodytext">
     Thank you for contacting us. We will be in touch with you very soon.</span>
    <?
    }
    ?>
    the site says to put in <?php session_start(); ?> which i dont think i need to cause i already have a session started.

    and then to add the following for error checking
    Code:
    include_once $_SERVER['DOCUMENT_ROOT'] . '/securimage/securimage.php';
    
    $securimage = new Securimage();
    in addition

    Code:
    if ($securimage->check($_POST['captcha_code']) == false) {
      // the code was incorrect
      // handle the error accordingly with your other error checking
    
      // or you can do something really basic like this
      die('The code you entered was incorrect.  Go back and try again.');
    }
    This is a link to the site i'm referring to http://www.phpcaptcha.org/documentation/quickstart

    thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •