如果联系表单输出

时间:2016-02-25 17:54:57

标签: php html forms spam-prevention

我有一个带有安全问题的简单表单,以防止发送垃圾邮件。 使用问题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),然后继续联系fail- 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');} 
?>

2 个答案:

答案 0 :(得分:1)

为什么不尝试这样简单的东西?

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']));
    if($antispam == '12' && isEmail($clientEmail) && $phone != '' && $message != ''   ){
        $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 {
        header('Location: contact-failed.html#fail');
    }

答案 1 :(得分:0)

我的问题是您需要在联系表单中显示另一页而不是错误消息。您在成功时重定向到另一个页面,同样可以应用于错误,您可以重定向到另一个html页面以显示不同的版本。