PHP表单 - 如果蜜罐输入字段已填写 - 重定向到另一个页面

时间:2013-08-08 16:35:18

标签: php forms honeypot

我有一个简单的联系表单,我已经包含了一个蜜罐输入字段。 如果字段已填写,我希望表单重定向到网页。

我尝试了以下代码,但它给了我一个错误: AJAX请求失败了!

所以我知道我做错了什么。我确信这很简单。

感谢

php代码:

    if(!empty($_POST["e-mail"])) header('Location: blankman.html');exit;

表单输入:

    <input type="text" name="e-mail" id="e-mail"/>

这是完整的PHP代码:

    <?php

if(!empty($_POST["e-mail"])) header('Location: blankman.html');exit;

// Clean up the input values
foreach($_POST as $key => $value) {
  if(ini_get('magic_quotes_gpc'))
    $_POST[$key] = stripslashes($_POST[$key]);

  $_POST[$key] = htmlspecialchars(strip_tags($_POST[$key]));
}

// Assign the input values to variables for easy reference
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];


// Test input values for errors
$errors = array();
if(strlen($name) < 2) {
  if(!$name) {
    $errors[] = "missing your name";
  } else {
    $errors[] = "your name must be 2 characters";
  }
}

if(!$email) {
  $errors[] = "missing your email";
} else if(!validEmail($email)) {
  $errors[] = "you must enter a valid email";
}
if(strlen($message) < 3) {
  if(!$message) {
    $errors[] = "missing your message";
  } else {
    $errors[] = "oops! your message is not long enough";
  }
}

if($errors) {
  // Output errors and die with a failure message
  $errortext = "";
  foreach($errors as $error) {
    $errortext .= "<li>".$error."</li>";
  }
$response = array(
    "success" => false,
    "content" => "<span class='failure'><ul>". $errortext ."</ul></span>"
);
die(json_encode($response));
}

// Send the email *********** enter your email address and message info ***
$to = "myemail@myemail.com"; 
$subject = "Website message from: $name";
$message = "From:\n$name\n\nEmail:\n$email\n\nMessage:\n$message";
$headers = "From: $email";

mail($to, $subject, $message, $headers);

// Die with a success message
$response = array(
    "success" => true,
    "content" => "<span class='success'><li>Thank you! Your message has been sent :).</li></span>"
);
die(json_encode($response));

// A function that checks to see if
// an email is valid
function validEmail($email)
{
   $isValid = true;
   $atIndex = strrpos($email, "@");
   if (is_bool($atIndex) && !$atIndex)
   {
      $isValid = false;
   }
   else
   {
      $domain = substr($email, $atIndex+1);
      $local = substr($email, 0, $atIndex);
      $localLen = strlen($local);
      $domainLen = strlen($domain);
      if ($localLen < 1 || $localLen > 64)
      {
         // local part length exceeded
         $isValid = false;
      }
      else if ($domainLen < 1 || $domainLen > 255)
      {
         // domain part length exceeded
         $isValid = false;
      }
      else if ($local[0] == '.' || $local[$localLen-1] == '.')
      {
         // local part starts or ends with '.'
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $local))
      {
         // local part has two consecutive dots
         $isValid = false;
      }
      else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
      {
         // character not valid in domain part
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $domain))
      {
         // domain part has two consecutive dots
         $isValid = false;
      }
      else if(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
                 str_replace("\\\\","",$local)))
      {
         // character not valid in local part unless
         // local part is quoted
         if (!preg_match('/^"(\\\\"|[^"])+"$/',
             str_replace("\\\\","",$local)))
         {
            $isValid = false;
         }
      }
      if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
      {
         // domain not found in DNS
         $isValid = false;
      }
   }
   return $isValid;
}

?>

这是javascript:

<script>
    $(document).ready(function () {
        $("#contactform").submit(function (e) {
            e.preventDefault();
            var t = $(this).attr("action");
            var n = $(this).serialize();
            $.post(t, n, null, "json").done(function (e) {
                if (e.success) {
                    $("#success").html(e.content);
                    $("#contactform,#error").hide()
                } else {
                    $("#error").html(e.content)
                }
            }).fail(function () {
                alert("The AJAX request failed!")
            })
        })
    })
</script>

1 个答案:

答案 0 :(得分:2)

如果蜜罐被填充,请不要显示任何不同的行为。通过这种方式,你会用大红色的尖叫声尖叫着:“这是一个蜜罐!调查并写下一个解决方法!”

始终以与常规方式相同的方式回复垃圾邮件请求

if(!empty($_POST["e-mail"])) {
    $response = array(
        "success" => true,
        "content" => "<span class='success'><li>Thank you! Your message has been sent :).</li></span>"
    );
    die(json_encode($response));
}