2013-01-02 90 views
0

全部, 我在我的頁面上有一個表單,用於發送電子郵件。在表單頁面我有以下代碼:使用reCaptcha後防止電子郵件垃圾郵件

<input type="text" name="Name" id="your_name" class="contact_form_input_text"> 
<input type="text" name="Email_Address" id="your_email" class="contact_form_input_text"> 
<input type="text" name="fill_me_out" id="fill_me_out"> 
<input type="button" value="Send" id="submit_contact_form_button"> 

第一個文本框是lamecaptcha,我檢查它在PHP端,以確保它不填寫。我也使用一些JS這個隱藏:

jQuery(function(){ 
    jQuery("#fill_me_out").hide(); 
}); 

然後我收到我的網頁下面的表單驗證使用jQuery驗證提交:

jQuery("#contact_form").validate({ 
    rules: { 
     Email_Address: { 
      required: true, 
      email: true 
     }, 
     Name: { 
      required: true 
     } 
    }, 
    messages: { 
     Email_Address: { 
      required: "Please enter an email address!", 
      email: "Please enter a valid email address!" 
     }, 
     Name: { 
      required: "Please enter your Name!" 
     } 
    } 
}); 

jQuery("#submit_contact_form_button").click(function(event) { 
     if (jQuery("#contact_form").valid()) { 
     challengeField = jQuery("input#recaptcha_challenge_field").val(); 
      responseField = jQuery("input#recaptcha_response_field").val(); 
      var html = jQuery.ajax({ 
      type: "POST", 
      url: site_url + "ajax.recaptcha.php", 
      data: "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField, 
      async: false 
      }).responseText; 

      if(html == "success") 
      { 
       //$("#captchaStatus").html(" "); 
       // Uncomment the following line in your application 
       //return true; 
       jQuery("#contact_form").submit(); 
      }else{ 
       jQuery("#captchaStatus").html("Your captcha is incorrect. Please try again"); 
       Recaptcha.reload(); 
       return false; 
      } 
     } 
     return false; 
    }); 

如果一切都正確填寫頁面,然後提交。我有以下的檢查,以檢查跛腳驗證碼:

$lamecaptcha_check = $_POST['fill_me_out']; 
if($lamecaptcha_check!=""){ 
    echo '[box style="alert"]Why are you trying to spam us? It could be because you don\'t have Javascript enabled and filled out an incorrect box![/box]'; 
}else{ 
    //Send the form using mail 
} 

提交表單是一個按鈕,而不是進行提交,它必須經過jQuery驗證做甚至提交。不知何故,我仍然收到空白的電子郵件通過。有誰知道我可以做什麼來防止垃圾郵件/空白電子郵件?我想我應該檢查後端的變量,以確保它們不是空白的,但是除非有一些值,否則表單甚至不應該被提交,所以我需要在初始頁面上有效的電子郵件地址。任何想法表示讚賞!

謝謝!

+0

我建議你不要通過迴應錯誤甚至調用一個輸入字段來通知「垃圾郵件」*「請填寫我」* –

+0

你總是可以動態地添加一個JS給兩個字段的特殊值,形成DOM創建時間和提交時間,而不是使用PHP檢查和評估這些字段之間的時間差。垃圾郵件通常很快。人類不是。 –

回答

1

只是因爲你有一個提交按鈕才能通過jQuery工作,並不意味着表單不能以其他方式提交。

垃圾郵件可能會檢查您的表單的HTML,查看不同的字段,然後發送POST請求與相關信息。它不會評估你的jQuery。

如果您想要做這樣的事情,請設置表格的action="javascript:;",然後在您提交之前在您的jQuery中將其更新爲實際值。

+0

感謝您的想法。你可以使用jQuery控制動作屬性字段,或者你只是更新它與document.submit()的位置? – user1048676

+0

只需使用'.attr()'來設置它。 –