I was wondering if people could take a look at this php script and help me make it more secure or make tweaks here and there. The script is below:
- Code: Select all
<?php
/*$mail_to = "........";
$mail_from = "..................";
$mail_subject = ".........";
$mail_start = "Message from Website: ";*/
if(!isset($_POST['send']))
header("location:../unable.html");
else
composeEmail();
function composeEmail()
{
$mail_to = ".....";
$mail_from = ".......";
$mail_subject = "..........";
$mail_start = "Message from Website: ";
$name = stripslashes($_POST['fname']);
//$name = stripslashes('Bob');
$email = stripslashes($_POST['email']);
//$email = stripslashes('whitewater456@googlemail.com');
$telephone = stripslashes($_POST['telephone']);
//$telephone = stripslashes('01667111111');
$message = stripslashes($_POST['message']);
//$message = stripslashes('This is a test to see if things are working');
$isValid = validateEmail($email);
if(!$isValid)
header("location:../unable.html");
$message_body = $mail_start."\n\n";
$message_body .= "Name: ".$name."\n";
$message_body .= "Email: ".$email."\n";
$message_body .= "Telephone: ".$telephone."\n\n\n";
$message_body .= "Message: ".$message;
//send email
if(mail($mail_to, $mail_subject, $message_body , "From: ".$mail_from."\r\n"))
header("location:../thankyou.html");
//else
//header("location:../unable.html");
} //end of function <composeEmail>
function validateEmail($emailAdd)
{
$emailAdd = filter_var($emailAdd, FILTER_SANITIZE_EMAIL);
if(filter_var($emailAdd, FILTER_VALIDATE_EMAIL))
return true;
else
return false;
} //end of function <validateEmail>
?>
I am doing validation in javascript before the form is submited but am not sure if i should do more in php as a fail safe and if stripslashes is enough to catch most problems.
UPDATE: Another issue I'm having is if i keep the mail to, from etc variables outside the function it throws an undefined error. I assume this is down to scope but I would assume if the variables are defined at the top of the file everything in the file should be able to see them.
Thank You
