2017-04-12 81 views
1

我是一個業餘的網頁設計師,我已經在stackoverflow.com和其他網站上搜索,並已找到這個問題我有很多修復,但他們都沒有工作(可能是因爲我實施他們不正確)。我希望有更多知識的人可以通過簡單的修復幫助我,或者告訴我如何實現我找到的修復之一。防止php web聯繫表格垃圾郵件

問題:我的業務網站上有一個非常簡單的php聯繫表單。它工作了很多年,但在上週遭到黑客入侵。我現在每天收到數百份聯繫表單提交,沒有評論,他們只有(顯然是有效的)電子郵件地址和名稱字段中的一串字符(如「58ee8b52eef46」)。

我已經嘗試了幾種技術來防止這種垃圾郵件,他們要麼打破我的PHP形式,要麼他們不阻止垃圾郵件。 如果可能的話,我想要一個不需要驗證碼失真文本測試的解決方案,並且不需要填寫表單的所有字段。

這裏是我的全部的PHP代碼:

<?php 
if(isset($_POST['email'])) { 
$email_to = "[email protected]"; 
$email_subject = "website form submission"; 

function died($error) { 
    echo "We are very sorry, but there were error(s) found with the form you submitted. "; 
    echo "These errors appear below.<br /><br />"; 
    echo $error."<br /><br />"; 
    echo "Please go back and fix these errors.<br /><br />"; 
    die(); 
} 

if(!isset($_POST['name']) || 
    !isset($_POST['email']) || 
    !isset($_POST['telephone']) || 
    !isset($_POST['comments'])) { 
    died('We are sorry, but there appears to be a problem with the form you submitted.');  
} 

$name = $_POST['name']; 
$email_from = $_POST['email']; 
$telephone = $_POST['telephone']; 
$comments = $_POST['comments']; 

$error_message = ""; 
if(strlen($error_message) > 0) { 
died($error_message); 
} 
$email_message = "Form details below.\n\n"; 

function clean_string($string) { 
    $bad = array("content-type","bcc:","to:","cc:","href"); 
    return str_replace($bad,"",$string); 
} 

$email_message .= "Name: ".clean_string($name)."\n"; 
$email_message .= "Email: ".clean_string($email_from)."\n"; 
$email_message .= "Telephone: ".clean_string($telephone)."\n"; 
$email_message .= "Comments: ".clean_string($comments)."\n"; 

$headers = 'From: '.$email_from."\r\n". 
'Reply-To: '.$email_from."\r\n" . 
'X-Mailer: PHP/' . phpversion(); 
@mail($email_to, $email_subject, $email_message, $headers); 
?> 

Thank you for contacting us. We will be in touch with you soon. You will now be redirected back to example.com. 
<META http-equiv="refresh" content="2;URL=http://www.example.com"> 


<?php 
} 
die(); 
?> 
+0

你有看'captcha' – RiggsFolly

+0

林我確定我有提及d此之前,但谷歌recaptcha現在是絕大多數合法訪問者無法訪問。 – Steve

+0

我已考慮使用驗證碼。但我更喜歡一個不使用captcha的修補程序,以便在我的網站上保持整潔。 – user7858610

回答

6

一個簡單的技巧是創建一個蜜罐領域:

HTML

<!-- within your existing form add this field --> 
<input type="text" id="website" name="website"/> 

CSS

/*in your css hide the field so real users cant fill it in*/ 
form #website{ display:none; } 

PHP

//in your php ignore any submissions that inlcude this field 
if(!empty($_POST['website'])) die(); 
+0

謝謝!這非常簡單並且易於實施。它符合我所有的標準。但是,目前我收到的垃圾郵件中的「評論」字段爲空。如果它也將「網站」字段留空,我是否仍然收到垃圾郵件? – user7858610

+0

是的,「網站」是一個很好的普遍選擇,因爲大多數愚蠢的垃圾郵件者正在尋求放棄鏈接。您可以根據需要添加儘可能多的蜜罐字段。您甚至可以添加名爲'email'的蜜罐,並將您的真實電子郵件字段重命名爲其他內容(如'emailaddress')。 – Steve

+0

我試圖做到這一點,並插入字段和隱藏他們工作得很好,但我找不到正確的位置在哪裏輸入PHP代碼(與死亡)到我的PHP文件。它只是在任何地方停止,即使這些字段沒有填寫。 – vega

0

如果您看到了沒有註釋的垃圾,爲什麼不直接添加該支票?絕對沒有理由讓真正的人類訪問者在沒有評論的情況下提交您的聯繫表單。

由於您不想添加驗證碼,短期內最簡單的解決方案是檢查評論是否爲最少字符數,並且包含至少一定數量的單詞。

例如:

$comments = trim($_POST['comments']); // trim() to strip off whitespace from beginning and end, like spaces and linebreaks 

if (strlen($comments) < 20 || substr_count($comments, " ") < 3) { 
    died('Your comment is too short.'); 
} 

這是一個非常簡單的檢查以查看註釋包含至少20個字符和至少3個位(4個字)。根據需要調整。

+0

嗨,謝謝你的答案。我已經實施了與您的類似的另一個修復程序。然而,我們偶爾會從僅僅想知道更多信息的人那裏獲得表單提交,並且只包含沒有評論的電子郵件或電話號碼。實際上,我們已將此類潛在客戶轉化爲銷售業務,因此我不想刪除沒有評論的情況下接收表單的可能性。我希望這是有道理的。 – user7858610

+0

有道理,但這確實爲垃圾郵件機器人打開了大門。在這種情況下,您可能需要考慮添加「只發送給我信息」複選框並默認禁用提交按鈕(以便機器人無法提交表單),然後在輸入評論時使用javascript啓用**或**用戶檢查該複選框。 – rickdenhaan