0

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

<?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 中完成这项工作?

4

2 回答 2

1
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,然后设置一个会话变量(让 bot 认为它完成了它的工作,类似于email_sent=trueor success=true)并在成功页面中使用该变量,您将else case在 bot 提交表单的地方执行此操作。

于 2013-03-02T23:16:24.210 回答
1

你的意思是发送带有字段的消息吗?
尝试这个:

<?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>
于 2013-03-02T23:35:09.430 回答