【发布时间】:2016-06-08 15:57:55
【问题描述】:
我有一个带有安全问题的简单表单以防止垃圾邮件。 使用相关表单链接到我的网页http://ddry.co.uk/contact_us.html。
如果用户输入不正确的答案而不仅仅是纯文本,我希望能够输出一个 html 页面。
如果表单在我的 php 脚本底部成功,我将重定向到另一个 html 文件。查看有人建议使用 readfile("contact-failed.html#fail"); 的其他论坛显示html;但是,我不完全确定将错误答案重定向的代码放在哪里。我是 PHP 新手,所以如果有人能够解释我做错了什么,那就太好了。或者,如果 somone 有更好的垃圾邮件过滤器代码,那就太好了。提前谢谢。
反垃圾邮件html代码
用于发布的 php 文件。
----- 更新 --------
我想我要的是 if, else 语句? 经过研究,我更改了代码以包含 else 语句;但是,由于我缺乏 PHP 知识,我仍然得到一个空白屏幕,而不是我的错误重定向 html 页面,该页面显示在我的 php 代码的底部。
问题:如何正确配置 if、else 语句,以便如果反垃圾邮件结果错误(不等于 12)然后继续进行 contact-failed.html?
提前致谢
<?php
// Email address verification
function isEmail($clientEmail) {
return filter_var($clientEmail, FILTER_VALIDATE_EMAIL);}
if($_POST) {
// Enter the email where you want to receive the message
$myemail = 'info@ddry.co.uk';
$name = addslashes(trim($_POST['name']));
$clientEmail = addslashes(trim($_POST['email']));
$subject = addslashes(trim($_POST['phone']));
$phone = addslashes(trim($_POST['phone']));
$message = addslashes(trim($_POST['message']));
$antispam = addslashes(trim($_POST['antispam']));
$array = array('nameMessage' => '','emailMessage' => '', 'phoneMessage' => '', 'messageMessage' => '', 'antispamMessage' => '');
if(!isEmail($clientEmail)) {
$array['nameMessage'] = 'Empty name';
}
if(!isEmail($clientEmail)) {
$array['emailMessage'] = 'Invalid email!';
}
if($phone == '') {
$array['phoneMessage'] = 'Empty phone number!';
}
if($message == '') {
$array['messageMessage'] = 'Empty message!';
}
if($antispam != '12') {
$array['antispamMessage'] = 'Incorrect Answer!';
}
if(isEmail($clientEmail) && $clientEmail != '' && $message != '' && $antispam == '12') {
// Send email
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $clientEmail\n Message: \n $message\n Phone: $phone";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $clientEmail";
mail($to,$email_subject,$email_body,$headers);
echo json_encode($array);
header('Location: contact-success.html#success');
}
else (isEmail($clientEmail) && $clientEmail != '' && $message != '' && $antispam !== '12'){
echo('Location: contact-failed.html#fail');}
?>
【问题讨论】:
-
试试这样的: if ($antispam != '12') { header("Location: errorpage.html"); }
标签: php html forms spam-prevention