ndwest12
09-18-2009, 04:50 PM
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.
<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:
<?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
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.
<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:
<?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