【问题标题】:Php if statements/contact form anti spam integrationPHP if 语句/联系表单反垃圾邮件集成
【发布时间】:2014-08-25 22:55:33
【问题描述】:

我正在尝试将this tutorial 整合到我现有的联系表单中。但是,如果它不起作用。基本上我想要一个用 CSS 隐藏的空白 url 输入字段。如果该字段被填写,表格将不会发送。这是我原来的邮件脚本:

<?php
// My modifications to mailer script from:
// http://blog.teamtreehouse.com/create-ajax-contact-form
// Added input sanitizing to prevent injection

// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get the form fields and remove whitespace.
    $name = strip_tags(trim($_POST["name"]));
            $name = str_replace(array("\r","\n"),array(" "," "),$name);
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
    $message = trim($_POST["message"]);

    // Check that data was sent to the mailer.
    if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        // Set a 400 (bad request) response code and exit.
        http_response_code(400);
        echo "Oops! There was a problem with your submission. Please complete the form and try again.";
        exit;
    }

    // Set the recipient email address.
    // FIXME: Update this to your desired email address.
    $recipient = "rrswans@gmail.com";

    // Set the email subject.
    $subject = "New contact from $name";

    // Build the email content.
    $email_content = "Name: $name\n";
    $email_content .= "Email: $email\n\n";
    $email_content .= "Message:\n$message\n";

    // Build the email headers.
    $email_headers = "From: $name <$email>";

    // Send the email.
    if (mail($recipient, $subject, $email_content, $email_headers)) {
        // Set a 200 (okay) response code.
        //http_response_code(200);
        echo "Thank You! Message Received! Click to close. ";
    } else {
        // Set a 500 (internal server error) response code.
        //http_response_code(500);
        echo "Oops! Something went wrong and we couldn't send your message.";
    }

} else {
    // Not a POST request, set a 403 (forbidden) response code.
   // http_response_code(403);
    echo "There was a problem with your submission, please try again.";
}

?>

这是我需要集成的代码...

<?php 
// if the url field is empty 
if(isset($_POST['url']) && $_POST['url'] == ''){
     // then send the form to your email
      mail( 'you@yoursite.com', 'Contact Form', print_r($_POST,true) ); 
} 
// otherwise, let the spammer think that they got their message through
?>

我确信这很容易,我只是不太了解 PHP。任何帮助,将不胜感激。

【问题讨论】:

    标签: php email sendmail


    【解决方案1】:

    尝试改变:

        if (mail($recipient, $subject, $email_content, $email_headers)) {...
    

    收件人:

        if (isset($_POST['url']) && $_POST['url'] == '' &&
           mail($recipient, $subject, $email_content, $email_headers)) {...
    

    此外,您的隐藏输入被称为“蜜罐”,如果设置为:

    position:absolute;
    top: -100;
    

    而不是

    display:none
    

    编辑

    我忘记了 if 语句是从左到右验证的,请参阅上面的修订。 还要确保你的输入被命名为'url'

    <input name="url" />
    

    【讨论】:

    • 好吧,让表单发送,但不阻止它在“蜜罐”中有文本时不发送
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-05
    • 2015-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多