2016-02-25 62 views
0

我有一個帶有安全問題的簡單表單以防止垃圾郵件。 以問題http://ddry.co.uk/contact_us.html的形式鏈接到我的網頁。如果在聯繫表格輸出中發生錯誤,PHP將顯示一個html頁面

我希望能夠輸出一個HTML頁面,如果用戶輸入一個不正確的答案,而不是純文本。

我有一個重定向到另一個html文件,如果表單成功在我的PHP腳本的底部。看着有人建議使用readfile的其他論壇(「contact-failed.html#fail」);顯示html;不過,我並不完全確定將錯誤答案的重定向代碼放在哪裏。我是PHP新手,所以如果有人能夠解釋我做錯了什麼,那會很好。或者,如果somone擁有更好的垃圾郵件過濾器代碼,那麼這也是非常有用的。

反垃圾郵件的html代碼

PHP文件的後。

----- UPDATE --------

我覺得我後是一個if,else語句? 研究後,我改變了我的代碼,包括一個else語句;然而,由於我缺乏PHP知識,我仍然得到一個空白屏幕,而不是我的錯誤重定向html頁面,它顯示在我的PHP代碼的底部。

問題:如何正確配置if,else語句,如果反垃圾郵件結果錯誤(不等於12),請繼續聯繫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 = '[email protected]'; 

$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');} 
?> 
+0

嘗試是這樣的: 如果($反垃圾郵件= '12'!) { 標題( 「位置:errorpage.html」); } – SamyQc

回答

1

爲什麼不嘗試這樣簡單的事情?

function isEmail($clientEmail) { 
    return filter_var($clientEmail, FILTER_VALIDATE_EMAIL); 
} 

if($_POST){ 
    // Enter the email where you want to receive the message 
    $myemail = '[email protected]'; 
    $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'); 
    } 
+0

謝謝SamyQc !!!我改變了: echo('Location:contact-failed.html#fail'); for header('Location:contact-failed.html#fail'); 它工作出色。感謝您的輸入:-) – Andy14

+0

很高興幫助:)接受我的答案,如果它做了你所要求的!再見 – SamyQc

0

我的問題是,爲什麼你需要表現出另一個頁面,而不是在接觸形式的錯誤消息。您正在重定向到另一個成功的頁面,同樣的錯誤也可以應用,您可以重定向到另一個html頁面以顯示不同的版本。

+0

嗨Kiran。謝謝回覆。這正是我想要的,問題是我缺乏實現這一點的PHP知識。如果推算出錯誤的答案,我該如何實現重定向?成功重定向很簡單,但是我會在何處以及如何在錯誤答案中添加fail.html文件重定向?謝謝 – Andy14

相關問題