2电子邮件联系表单使用PHP中的安全代码进行验证

时间:2017-04-28 20:44:32

标签: php forms email contact-form

我无法正常工作。

我想要发生的是有人能够填写给定的信息,但是有电子邮件验证,只是您将电子邮件放入电子邮件:并且假设在验证电子邮件中匹配:然后在希望他们这样写,如果他们回答垃圾邮件问题权利只是"什么是5 + 5?"然后表格将发送到我的电子邮件

问题在于,虽然电子邮件验证工作正常,但即使您将答案写错了垃圾邮件问题,表单仍然会被发送。我需要它,以便垃圾邮件和电子邮件验证要么或必须是正确的,否则表格不会发送。

我希望让一些人了解并帮助我这么简单!这是我的PHP代码。让我知道我做错了什么。

<script>


<?php

$email1=$_POST['Email1'];
$email2=$_POST['Email2'];
$from=$_POST['Email1'];
$email="!YOUREMAIL@GOES.HERE!";
$subject="maintenance Request";
$message=$_POST['Box'];
$select=$_POST['Select'];
$name=$_POST['Name'];
$number=$_POST['Question'];
$message="Name:   ".$name. "\r\n" ."\r\n" . "Email:   " .$from ."\r\n" . "\r\n" . "Comment:   " .$message ."\r\n" . "\r\n" . "Selected Machine:   " .$select;


if($number==10) {

}


if(filter_var($email1, FILTER_VALIDATE_EMAIL)) {
}
if (filter_var($email2, FILTER_VALIDATE_EMAIL)) {
mail ($email, $subject, $message, "from:".$from);
echo 'Thanks You for the Maintenance Request! We will Contact you shortly. ';
}
else {
    echo "This ($email2) email address is different from ($email1).\n";
}

?>
</script>

1 个答案:

答案 0 :(得分:1)

如果验证问题的答案有误,则应打印一条消息并退出脚本。

if ($number != 10) {
    die("You are not a human!");
}

您也永远不会检查这两封电子邮件是否相同。

if(!filter_var($email1, FILTER_VALIDATE_EMAIL)) {
    die("Invalid email ($email1)");
}
if ($email1 == $email2) {
    mail ($email, $subject, $message, "from:".$from);
    echo 'Thanks You for the Maintenance Request! We will Contact you shortly. ';
}
else {
    echo "This ($email2) email address is different from ($email1).\n";
}

无需在filter_var()上致电$email2。如果它等于$email1,则它必须有效。如果它不等于$email1,我们就不在乎它是否有效。