Nwo*_*rks 0 php email contact-form secure-coding
你好!我只是想知道我刚刚创建的这个contactform脚本有多安全?很久以前,当我制作我的联系表时,我的老师正在唠叨我.
if($_SERVER['REQUEST_METHOD'] === 'POST'){
$myemail = "email@adress.com";
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$comments = $_POST['comments'];
if($name == 0 || !preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email) || !preg_match("/^\d{2}(-\d{3}){2}(\d{2})?$/", $phone) || $subject == 0 || $comments == 0){
$error_message = 'Something was written wrong..';
} else {
$message = "Hello!
Your contact form has been submitted by:
Name: $name
E-mail: $email
Phone: $phone
Comments: $comments
End of message";
mail($myemail, $subject, $message);
$error_message = 'Your message was sent!';
}
}
Run Code Online (Sandbox Code Playgroud)
有关如何使其安全的任何建议?
您可以使用函数来验证条目,例如:
function check_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
Run Code Online (Sandbox Code Playgroud)
和
$name = check_input($_POST['name']);
$email = check_input($_POST['email']);
$phone = check_input($_POST['phone']);
$subject = check_input($_POST['subject']);
$comments = check_input($_POST['comments']);
Run Code Online (Sandbox Code Playgroud)
和
if ($name && $email && $phone && $subject && $comments) {
Send contact form...
}
Run Code Online (Sandbox Code Playgroud)
当然,您可以添加验证码以使其更安全.