我读了这篇文章:什么是好的隐形验证码?关于使用网络表单中的隐藏字段来阻止基本机器人通过您的网站表单邮件向您的网站投掷垃圾邮件。我目前正在使用 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 中完成这项工作?