/    Sign up×
Community /Pin to ProfileBookmark

Blank Contact Form Submission – Suspected SPAM

Hello all,

I have a contact form on my website and I keep getting blank submissions from it which I suspect are SPAM.

Here is the php code I use to stop SPAM, can anyone see where it is going wrong or suggest what can be added to stop these blank submissions.

<?php
$to = “[email protected]“;
$subject = “Customer Contact Phone”;
$body = $_POST[“realname”];
$body .= “n”;
$body .= $_
POST[“number”];
$email = “[email protected]“;

function is_valid_email($to) {
return preg_match(‘#^[a-z0-9.!#$%&’*+-/=?^_`{|}~]+@([0-9.]+|([^s]+.+[a-z]{2,6}))$#si’, $to);
}

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 (!is_valid_email($to)) {
echo ‘Invalid email submitted – mail not being sent.’;
exit;
}

contains_bad_str($email);
contains_bad_str($subject);
contains_bad_str($body);
contains_bad_str($to);

contains_newlines($email);
contains_newlines($subject);
contains_newlines($to);

$headers = “From: $email”;
mail($to, $subject, $body, $headers);
echo “”;
die();
?>

Thanks in advance for any help with this!

Kind regards, Mike

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@Jarrod1937Jan 17.2011 — If they're blank just test that the string length (using strlen() ) is greater than 0, else trigger an error.
Copy linkTweet thisAlerts:
@sss_1234authorJan 17.2011 — Hi Jarrod, thanks very much for the reply.

I'll do some research on strlen() and give it a go! I'm new to all this so may have to post back if I can't get it to work!

Thanks again,

Mike
×

Success!

Help @sss_1234 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...