[英]Php if statements/contact form anti spam integration
我試圖將本教程集成到我現有的聯系表中。 但是,如果它不起作用。 基本上,我想使用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。 任何幫助,將不勝感激。
嘗試更改:
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" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.