PHP表单邮件使用不可见的表单字段来过滤机器人



我读过这篇文章:什么是好的隐形captcha?关于使用网络表单中的隐藏字段来阻止基本机器人通过您的网站表单邮件向您的网站发送垃圾邮件。我目前正在使用php脚本来处理我的表单邮件。我按照我找到的"bullet proff web表单"教程构建了这个脚本。它看起来像这样:

<?php
// Pick up the form data and assign it to variables
$name = $_POST['name'];
$email = $_POST['email'];
$topic = $_POST['topic'];
$comments = $_POST['comments'];
// Build the email (replace the address in the $to section with your own)
$to = 'hello@cipherbunny.com';
$subject = "New message: $topic";
$message = "$name said: $comments";
$headers = "From: $email";
// Data cleaning function
  function clean_data($string) {
  if (get_magic_quotes_gpc()) {
  $string = stripslashes($string);
  }
  $string = strip_tags($string);
  return mysql_real_escape_string($string);
}
// Mail header removal
function remove_headers($string) { 
  $headers = array(
    "/to:/i",
    "/from:/i",
    "/bcc:/i",
    "/cc:/i",
    "/Content-Transfer-Encoding:/i",
    "/Content-Type:/i",
    "/Mime-Version:/i" 
  ); 
  $string = preg_replace($headers, '', $string);
  return strip_tags($string);
} 
// Pick up the cleaned form data
$name = remove_headers($_POST['name']);
$email = remove_headers($_POST['email']);
$topic = remove_headers($_POST['topic']);
$comments = remove_headers($_POST['comments']);
// Send the mail using PHPs mail() function
mail($to, $subject, $message, $headers);
// Redirect
header("Location: http://foobar/success.html"); 

我想修改这个脚本,这样,如果填写了一个标识符为"other_email"的隐藏字段,那么表单电子邮件就不会被发送。我想这就像把上面的代码包装在if语句中检查字段是否完整一样简单。我已经尝试将其添加到"//拾取表单数据并将其分配给变量"代码下:

$testBot = $_POST['other_email'];

然后写入:

if(other_email == "") //If other_email form section is blank then... 
{
    run all the code above inserted here;
}
else
{
 Don't know what I should put here to stop it posting, yet still show the success form so 
 the spam bot don't know 
}

非常感谢您的帮助。我不得不说,我真的没有太多php知识,我才刚刚开始了解它,并认为表单邮件将是一个好的开始。

我如何在PhP中做到这一点?

if(other_email == "") //If other_email form section is blank then... 
{
    run all the code above inserted here;
}
else
{
 header("Location: http://foobar/success.html");
}

保持简单,对你有用。。

实际上,它会

  • 不要向你提交/邮寄任何东西。。。所以没有垃圾邮件
  • 一个简单的机器人会照它做的那样

如果你可以在成功页面上使用php,然后设置一个会话变量(让机器人认为它完成了自己的工作,比如email_sent=truesuccess=true(,并在成功页面中使用该变量,你将在机器人提交表单的else case中进行。

您的意思是用字段发送消息吗
试试这个:

<?php
// Pick up the form data and assign it to variables
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$topic = $_REQUEST['topic'];
$comments = $_REQUEST['comments'];
// Build the email (replace the address in the $to section with your own)
if($name !== null && $email !== null && $topic !== null && $comments !== null){
$to = 'hello@cipherbunny.com';
$subject = "New message: $topic";
$message = "$name said: $comments";
$headers = "From: $email";
// Data cleaning function
  function clean_data($string) {
  if (get_magic_quotes_gpc()) {
  $string = stripslashes($string);
  }
  $string = strip_tags($string);
  return mysql_real_escape_string($string);
}
// Mail header removal
function remove_headers($string) { 
  $headers = array(
    "/to:/i",
    "/from:/i",
    "/bcc:/i",
    "/cc:/i",
    "/Content-Transfer-Encoding:/i",
    "/Content-Type:/i",
    "/Mime-Version:/i" 
  ); 
  $string = preg_replace($headers, '', $string);
  return strip_tags($string);
} 
// Pick up the cleaned form data
$name = remove_headers($_POST['name']);
$email = remove_headers($_POST['email']);
$topic = remove_headers($_POST['topic']);
$comments = remove_headers($_POST['comments']);
// Send the mail using PHPs mail() function
mail($to, $subject, $message, $headers);
// Redirect
header("Location: http://foobar/success.html"); 
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=shift_jis" />
<title>Send</title>
</head>
<body>
<form action="#" method="POST">
Name     : <input type="text" name="name" /><br />
Email    : <input type="text" name="email" /><br />
Topic    : <input type="text" name="topic" /><br />
Comments : <textarea name="comments"></textarea><br />
<input type="submit" value="Send" />
</form>
</body>
</html>

最新更新