How do I spam proof my contact form?
Hello everybody,
I have a contact form that I've been using on my site. More and more often I receive spam email from it and I'd like some advise on making it spam proof.
All the spam emails contain hyperlinks so I thought a first step might be to have the form object to any field containing 'href'.
The PHP code of the form looks like this:
PHP Code:
<?
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
if ($_POST["name"] and $_POST["email2"] and $_POST["message"]){
$extra = "?sent=contact";
$name = $_POST['name'];
$email = $_POST['email2'];
$message = $_POST['message'];
$mailing = $_POST['mailing'];
$to = "me@mysite.com";
$subject = "MySite // Contact Form";
$body = "\r\nHello,\r\n\r\nHere is a message from the contact form: \r\n\r\n";
$body .= "Message: - \r\n".$message."\r\n\r\n";
$body .= "Name: ".$name."\r\n\r\n";
$body .= "Email: ".$email."\r\n";
if ($mailing =="Join Mailing List") {
$body .= "Please add this email to the list: \r\n";
}
$body .= "\r\nMessage ends dude!\r\n\r\nPeace out!\r\nDOG.DC5B Mailer";
$from = "From: MySite Mailer <mailer@mysite.com>";
mail($to, $subject, $body, $from, "-fmailer@dmysite.com");
}
else {
$extra = "?sent=no-contact";
}
header("Location: http://$host$uri/$extra");
exit;
?>
Thanks for any help,
Monkeyzbox
How do I spam proof my contact form?
http://www.w3schools.com/php/php_secure_mail.asp
This is where i got this script. It basically checks to see if the email is valid, and if not, it will give a kick back. i dont know if this will help, but id figure i would give it a try.
PHP Code:
<html>
<body>
<?php
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) {
echo "Invalid input";
} else { //send email
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail("someone@example.com", "Subject: $subject",
$message, "From: $email" );
echo "Thank you for using our mail form";
}
} else { //if "email" is not filled out, display the form
echo "<form method='post' action='mailform.php'>
Email: <input name='email' type='text' /><br />
Subject: <input name='subject' type='text' /><br />
Message:<br />
<textarea name='message' rows='15' cols='40'>
</textarea><br />
<input type='submit' />
</form>";
}
?>
</body>
</html>