Hi all!
I have a simple contact us form that was working in an earlier version of PHP. My hosts have upgraded to PHP Version 5.3.3-7+squeeze13 and as a result I had to change the email validation check to preg_match from !ereg.
Now I have another problem!
I’m getting a message “Please go back and type a Message” in the message field even though a valid message has been entered.
Here’s the code:
[php]<?php
if (isset($_POST[“op”]) && ($_POST[“op”]==“send”)) {
/******** START OF CONFIG SECTION /
$sendto = “xxxxxxxxxxxx”;
$subject = “Website Enquiry”;
// Select if you want to check form for standard spam text
$SpamCheck = “Y”; // Y or N
$SpamReplaceText = “content removed”;
// Error message prited if spam form attack found
$SpamErrorMessage = “<p align=“center”><font color=“red”>Malicious code content detected.
Your IP Number of “.getenv(“REMOTE_ADDR”).” has been logged.
/* END OF CONFIG SECTION *******/
$name = $HTTP_POST_VARS[‘name’];
$email = $HTTP_POST_VARS[‘email’];
$home_phone = $HTTP_POST_VARS[‘home_phone’];
$mobile_phone = $HTTP_POST_VARS[‘mobile_phone’];
$message = $HTTP_POST_VARS[‘message’];
$headerwinters = “From: $email\n”;
$headerwinters . “MIME-Version: 1.0\n”
. “Content-Transfer-Encoding: 7bit\n”
. “Content-type: text/html; charset = “iso-8859-1”;\n\n”;
if ($SpamCheck == “Y”) {
// Check for Website URL’s in the form input boxes as if we block website URLs from the form,
// then this will stop the spammers wastignt ime sending emails
if (preg_match("/http/i", “$name”)) {echo “$SpamErrorMessage”; exit();}
if (preg_match("/http/i", “$email”)) {echo “$SpamErrorMessage”; exit();}
if (preg_match("/http/i", “$home_phone”)) {echo “$SpamErrorMessage”; exit();}
if (preg_match("/http/i", “$mobile_phone”)) {echo “$SpamErrorMessage”; exit();}
if (preg_match("/http/i", “$message”)) {echo “$SpamErrorMessage”; exit();}
// Patterm match search to strip out the invalid charcaters, this prevents the mail injection spammer
$pattern = ‘/(;|||`|>|<|&|^|"|’."\n|\r|’".’|{|}|[|]|)|()/i’; // build the pattern match string
$name = preg_replace($pattern, “”, $name);
$email = preg_replace($pattern, “”, $email);
$home_phone = preg_replace($pattern, “”, $home_phone);
$mobile_phone = preg_replace($pattern, “”, $mobile_phone);
$message = preg_replace($pattern, “”, $message);
// Check for the injected headerwinters from the spammer attempt
// This will replace the injection attempt text with the string you have set in the above config section
$find = array("/bcc:/i","/Content-Type:/i","/cc:/i","/to:/i");
$email = preg_replace($find, “$SpamReplaceText”, $email);
$name = preg_replace($find, “$SpamReplaceText”, $name);
$home_phone = preg_replace($find, “$SpamReplaceText”, $home_phone);
$mobile_phone = preg_replace($find, “$SpamReplaceText”, $mobile_phone);
$message = preg_replace($find, “$SpamReplaceText”, $message);
// Check to see if the fields contain any content we want to ban
if(stristr($name, $SpamReplaceText) !== FALSE) {echo “$SpamErrorMessage”; exit();}
if(stristr($message, $SpamReplaceText) !== FALSE) {echo “$SpamErrorMessage”; exit();}
// Do a check on the send email and subject text
if(stristr($sendto, $SpamReplaceText) !== FALSE) {echo “$SpamErrorMessage”; exit();}
if(stristr($subject, $SpamReplaceText) !== FALSE) {echo “$SpamErrorMessage”; exit();}
}
// Build the email body text
$emailcontent = "
WEBSITE ENQUIRY
Name: $name
Home Phone: $home_phone
Mobile Phone: $mobile_phone
Email: $email
Message: $message
End of Email
“;
// Check the email address enmtered matches the standard email address format
if (preg_match(”^[A-Z0-9.%-]+@[A-Z0-9.%-]+.[A-Z]{2,6}$", $email)) {
echo “
It appears you entered an invalid email address
”;}
elseif (!trim($name)) {
echo “
Please go back and type a Message
”;}
elseif (!trim($message)) {
echo “
Please go back and enter an Email
”;}
elseif (!trim($email)) {
echo “
Please go back and enter an Email
”;}
// Sends out the email or will output the error message
elseif (mail($sendto, $subject, $emailcontent, $headerwinters)) {
echo "
Thank you $name for making an enquiry.
We will contact you as soon as possible. Regards, Craig and Anja
";}
}
else {
?>[/php]
Please can you tell my why it’s not working as it should. Many thanks.