Check for header injection attack and spam in your php form script

I wrote this to check for header injection attempts when processing a form that sends an email with php. (and I'll put it here so I can find it)

function is_spam( $message, $threshold = 1 ){
return _count_spam_body($message, $threshold) >= $threshold;
}

function _count_spam_body( $message, $threshold = 1 ){

$message = trim(strtolower($message));

//check the body of the message for spam or header injection attempts
$matches_bad = array(
"bcc:",
"cc:",
"to:",
"content-type:",
"mime-version:",
"multipart/mixed",
"content-transfer-encoding:",
"viagra",
);

$spam = 0;

foreach($matches_bad as $str){
if(strstr($message, $str)){
$spam++;
}
if($spam >= $threshold){
//stop counting if it's above the threshold
break;
}
}

return $spam;
}




Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options