optimus203
12-21-2010, 10:50 PM
Trying to prevent spam here. I need a function that will spit out an error message if http:// or www or <a href> is entered in message body. Any ideas?
Here is the form I've got now.
<?php
$name = $_POST['name'] ;
$email = $_POST['email'] ;
$phone = $_POST['phone'] ;
$find = $_POST['find'] ;
$subject = $_POST['subject'] ;
$message = $_POST['message'] ;
$verify = $_POST['verify'] ;
$body = "From: $name\n Email: $email\n Phone: $phone\n How They Found The Site: $find\n\n Message: $message";
function is_valid_email($email) {
return preg_match('#^[a-z0-9.!\#$%&\'*+-/=?^_`{|}~]+@([0-9.]+|([^\s]+\.+[a-z]{2,6}))$#si', $email);
}
function contains_bad_str($str_to_test) {
$bad_strings = array(
"content-type:"
,"mime-version:"
,"multipart/mixed"
,"Content-Transfer-Encoding:"
,"bcc:"
,"cc:"
,"to:"
);
foreach($bad_strings as $bad_string) {
if(eregi($bad_string, strtolower($str_to_test))) {
echo "$bad_string found. Suspected injection attempt - mail not being sent.";
exit;
}
}
}
function contains_newlines($str_to_test) {
if(preg_match("/(%0A|%0D|\\n+|\\r+)/i", $str_to_test) != 0) {
echo "newline found in $str_to_test. Suspected injection attempt - mail not being sent.";
exit;
}
}
if($_SERVER['REQUEST_METHOD'] != "POST"){
echo("Unauthorized attempt to access page.");
exit;
}
if(!is_valid_email($email)) {
echo '<h1 class="headerTop">Sorry. An Contact Form Error Has Occured</h1>
<p>Be sure to enter your name, email address, and a message. All fields marked with <strong>*</strong> are required.</p>
<p><a href="http://www.website.com">Click Here</a> to go back to contact form.</p>
<div class="spacer150"></div>';
exit;
}
contains_bad_str($email);
contains_bad_str($subject);
contains_bad_str(body);
contains_newlines($email);
contains_newlines($subject);
mail( "email@email.com", "Email from website - $subject", $body);
header( "Location: http://www.website.com/headerlocation.php" );
?>
I would also like to have a properly formatted error page for the if(!is_valid_email($email)) portion, but how would you point to a page like contacterror.php? I tried doing the following but did not work:
if(!is_valid_email($email)) {
include ('contacterror.php');
}
Here is the form I've got now.
<?php
$name = $_POST['name'] ;
$email = $_POST['email'] ;
$phone = $_POST['phone'] ;
$find = $_POST['find'] ;
$subject = $_POST['subject'] ;
$message = $_POST['message'] ;
$verify = $_POST['verify'] ;
$body = "From: $name\n Email: $email\n Phone: $phone\n How They Found The Site: $find\n\n Message: $message";
function is_valid_email($email) {
return preg_match('#^[a-z0-9.!\#$%&\'*+-/=?^_`{|}~]+@([0-9.]+|([^\s]+\.+[a-z]{2,6}))$#si', $email);
}
function contains_bad_str($str_to_test) {
$bad_strings = array(
"content-type:"
,"mime-version:"
,"multipart/mixed"
,"Content-Transfer-Encoding:"
,"bcc:"
,"cc:"
,"to:"
);
foreach($bad_strings as $bad_string) {
if(eregi($bad_string, strtolower($str_to_test))) {
echo "$bad_string found. Suspected injection attempt - mail not being sent.";
exit;
}
}
}
function contains_newlines($str_to_test) {
if(preg_match("/(%0A|%0D|\\n+|\\r+)/i", $str_to_test) != 0) {
echo "newline found in $str_to_test. Suspected injection attempt - mail not being sent.";
exit;
}
}
if($_SERVER['REQUEST_METHOD'] != "POST"){
echo("Unauthorized attempt to access page.");
exit;
}
if(!is_valid_email($email)) {
echo '<h1 class="headerTop">Sorry. An Contact Form Error Has Occured</h1>
<p>Be sure to enter your name, email address, and a message. All fields marked with <strong>*</strong> are required.</p>
<p><a href="http://www.website.com">Click Here</a> to go back to contact form.</p>
<div class="spacer150"></div>';
exit;
}
contains_bad_str($email);
contains_bad_str($subject);
contains_bad_str(body);
contains_newlines($email);
contains_newlines($subject);
mail( "email@email.com", "Email from website - $subject", $body);
header( "Location: http://www.website.com/headerlocation.php" );
?>
I would also like to have a properly formatted error page for the if(!is_valid_email($email)) portion, but how would you point to a page like contacterror.php? I tried doing the following but did not work:
if(!is_valid_email($email)) {
include ('contacterror.php');
}