I'm trying to create a form processing script that is hacker/spammer proof. Below are the steps that I'm using. Please provide comments about how it might be hacked and how I can improve it.
- At the beginning of the script,
$sendit=0;
$fakesend=0;
The recipient and subject line is specified in the script, not on the form.
The script checks the required fields to make sure that they are filled out. If a required field is not filled out, the user must go back and fill it in.
The script checks to make sure the form is POSTed.
if (strtoupper($_SERVER['REQUEST_METHOD']) != "POST") {
$sendit = 1;
$errormsg .= "Only POST method is allowed.<br>";
}
- The script checks to make sure there was data in the form.
if (!strlen($HTTP_POST_VARS)) {
$sendit = 1;
$errormsg .= "No data was provided.<br>";
}
- The script checks to make sure the submitter's email address is valid.
$pattern = "^([._a-z0-9-]+)@[a-z0-9-]+(\.[a-z0-9-]{2,})+$";
if(eregi($pattern,$_POST['Email'],$matches)){
$sendit = 0;
}else{
$sendit = 1;
$errormsg .= "Email address you submitted is invalid.<br>";
}
The script checks each field's data against a known list of spam words (mostly drugs, casinos, etc.). If an input contains a word that is a spam word,
$fakesend = 1.
The form checks to see if /r, /n, MIME-Version, or the domain name of the site was used in the form inputs.
if (eregi("\r",$name) || eregi("\n",$name) || eregi("MIME-Version:",$name) || eregi($emailsuffix,$name)){
$sendit = 1;
}
If $sendit = 1, the form displays an error to the user and what caused the error. If $sendit = 0, the script continues.
The message body is created.
$thebody .= htmlspecialchars(urldecode($key)).": ".htmlspecialchars(urldecode($value))."\n";
- If $fakesend=1, the script sends the user to the thank you page without sending out an email. If $fakesend=0, the script sends the email to the recipient and sends the user to the thank you page.
$hd = "From: ".$esend1."\r\n";
$hd .= "Reply-To: ".$esend1."\r\n";
$hd .= "Return-Path: ".$esend1."\r\n";
$hd .= "CC: \r\n";
$hd .= "BCC: \r\n";
if($fakesend == 0){
$emailsuccess = mail($erec1, $esub1, $ebod1, $hd);
}
header("Location: $thankpg");