2 电子邮件联系表格 PHP 中的安全代码验证

2 Email contact form Validation with security code in PHP

我无法正常工作。

我想要的是有人能够填写给定的信息,但是有一个电子邮件验证,就是你把你的电子邮件放在 Email: 中,并且假设在 Verify Email: 中匹配,然后希望他们写了那篇文章,如果他们正确回答了 "what is 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>

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

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";
}

无需在 $email2 上调用 filter_var()。如果它等于 $email1 那么它一定是有效的。如果它不等于 $email1,我们不关心它是否有效。