1

我假设它只是机器人,但我怎样才能阻止这种情况发生呢?我至少在这里进行了一些验证-输入和 php.ini 中的 HTML5“必需”。忘记客户端验证,如果我尝试在不填写信息的情况下发送它,它将返回一个错误(使用 php)。

如果我不能,机器人如何发送它?我需要添加什么?

<?php
    $allowedFields = array(
        'firstname',
    'lastname',
    'email',
    'phone',
    'address',
    'prefer-email',
    'prefer-snail ',
);

$requiredFields = array(
        'firstname',
    'lastname',
    'email',
    'phone',
    'address',
    'prefer-email',
    'prefer-snail ',
);

$requiredEmail = array(
    'email',
);

$errors = array();
foreach($_POST AS $key => $value) {
    // first need to make sure this is an allowed field
    if(in_array($key, $allowedFields)) {
    $$key = $value;

    // is this a required field?
    if(in_array($key, $requiredFields) && $value == '') {
        $errors[] = "The field $key is required.";
    }

    // is this a required field?
    if(in_array($key, $requiredEmail) && !filter_var($value, FILTER_VALIDATE_EMAIL))    {
        $errors[] = "A valid email is required.";
    }
    }   
}

// were there any errors?
if(count($errors) > 0) {
    $errorString = '<div class="error2"><h1>There was an error with the form.</h1><br />';
    $errorString .= '<ul>';
    foreach($errors as $error) {
        $errorString .= "<li>$error</li>";
    }
    $errorString .= '</ul></div>';

    // display the previous form
    include 'index.php';
} else {
    $first_name = $_POST['firstname'];
    $last_name = $_POST['lastname'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $address = $_POST['address'];
    $preferemail = $_POST['prefer-email'];
    $prefersnail = $_POST['prefer-snail'];


    $formcontent = "$firstname $lastname \r\n$email \r\n$phone \r\n$address \r\nPrefer Samples By: $preferemail $prefersnail \r\n";
    //$recipient = "Tester <test@test.com>";
    $recipient = "Tester <test@test.com>";
    $subject = "Test.com Form Submission";
    //$mailheader = "Landing Page <test@test.com>";
    mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
        header("Location: http://www.test.com/thanks.html");
}
4

1 回答 1

2

您只检查 $_POST 变量中的错误。机器人可能正在使用 GET 提交表单,因此回避了您设置的验证,该验证依赖于 $_POST 变量的存在。

我建议验证所有请求变量:$_REQUEST。或者,您可以检查是否存在任何 post 变量,如果不存在,则返回错误 - 从而强制 POST。

于 2013-09-13T13:51:35.757 回答