I've done my first PHP form, it works, but I can't get the spam filtering to work. Here is my script, you'll see I commented out a second form of filtering I tried. I'm receiving this error:
Fatal error: Call to undefined function: filter_var() in /home/www/mysite/sendmail.php on line 32
Help fixing this, or other ways to spam filter are welcome.
<?php
$firstname = $REQUEST['firstname'] ;
$lastname = $REQUEST['lastname'] ;
$email = $REQUEST['email'] ;
$addressone = $REQUEST['address'] ;
$addresstwo = $REQUEST['addresstwo'] ;
$city = $REQUEST['city'] ;
$state = $REQUEST['state'] ;
$zip = $REQUEST['zip'] ;
$phone = $REQUEST['phone'] ;
$comment = $REQUEST['comment2'] ;
$emailbody = $_REQUEST['extracomments'] ;
$to = "me@mysite.com";
$subject = "Web site Form";
$message = "
First Name: $firstname\n
Last Name: $lastname\n
Email: <$email>\n
Address: $addressone\n
Address: $addreestwo\n
City: $city\n
State: $state\n
Zip: $zip\n
Phone: $phone\n
Comment Regarding: $comment\n
Comment: $emailbody";
function spamcheck($field)
{
//filter_var() sanitizes the e-mail
//address using FILTER_SANITIZE_EMAIL
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
//filter_var() validates the e-mail
//address using FILTER_VALIDATE_EMAIL
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
if (isset($REQUEST['email']))
{//if "email" is filled out, proceed
//check if the email address is invalid
$mailcheck = spamcheck($REQUEST['email']);
if ($mailcheck==FALSE)
{
header( "Location: http://www.mysite.org/contact-error.html" );
}
else
{//send email
mail($to, $subject, $message, "From: $email");
header( "Location: http://www.mysite.org/thankyou.html" );
}
}
else
{//if "email" is not filled out, display the form
header( "Location: http://www.mysite.org/contact-error.html" );
}
/*if (ereg( "[\r\n]", $firstname ) || ereg( "[\r\n]", $email )) {
echo "Firstname or email have breaking lines";
header( "Location: http://www.mysite.org/contact-error.html" );
}
if (!isset($_REQUEST['email'])) {
echo "email problem";
header( "Location: http://www.mysite.org/contact-error.html" );
}
elseif (empty($firstname) || empty($emailbody)) {
echo "empty firstname or emailbody";
header( "Location: http://www.mysite.org/contact-error.html" );
}
else {
mail($to, $subject, $message, "From: $email");
header( "Location: http://www.mysite.org/thankyou.html" );
}*/
?>