2012-09-07 70 views
2

我有這種形式,用戶向我發送一封電子郵件。我不知道這是否固定,或者與安全問題,如果SQL是參與只出現......此表格是否安全?

HTML:

<form id="form4" action="send_mic.php" name="form4" method="post" > 

      <textarea name="message4" cols="4" rows="4" id="message4" ></textarea><br /> 

      <input type="text" id="name4" name="name4" value="" /><br /> 

      <input type="text" id="email4" name="email4" value="" /><br /> 

      <input type="submit" value="" id="submit" /> 

</form> 

的jQuery:

<script type="text/javascript"> 
$(document).ready(function() { 
    $('#form4').ajaxForm({ 
     beforeSubmit: validate 
    }); 

    function validate(formData, jqForm, options) { 
     var name = $('input[name=name4]').fieldValue(); 
     var email = $('input[name=email4]').fieldValue(); 
     var message = $('textarea[name=message4]').fieldValue(); 

     if (!name[0]) { 
      alert('Please enter a value for name'); 
      return false; 
     } 
     if (!email[0]) { 
      alert('Please enter a value for email'); 
      return false; 
     } 
     if (!message[0]) { 
      alert('Please enter a value for message'); 
      return false; 
     } 

     else { 

     $("#content").fadeOut(1000, function() { 
      $(this).html("<img src='images/postauto3.png'/>").fadeIn(2000); 
     }); 

     var message = $('textarea[name=message4]').val(''); 
     var name = $('input[name=name4]').val(''); 
     var email = $('input[name=email4]').val(''); 

      } 
    } 

}); 



    </script> 

PHP:

<?php 
     if($_POST){ 
       $email = $_POST['email4']; 
       $name = $_POST ['name4']; 
       $message = $_POST ['message4']; 
       // response hash 
       $ajaxresponse = array('type'=>'', 'message4'=>''); 

       try { 
         // do some sort of data validations, very simple example below 
         $all_fields = array('name4', 'email4', 'message4'); 

         foreach($all_fields as $field){ 
           if(empty($_POST[$field])){ 
             throw new Exception('Required field "'.ucfirst($field).'" missing input.'); 
           } 
         } 

         // ok, if field validations are ok 
         // now Send Email, ect. 

         // let's assume everything is ok, setup successful response 
         $subject = "New Contact"; 
         //get todays date 
         $todayis = date("l, F j, Y, g:i a") ; 

         $message = " $todayis \n 
         Attention: \n\n 
         Please see the message below: \n\n 
         Email Address: $email \n\n 
         Message: $message \n\n 

         "; 

         $from = "From: $email\r\n"; 


         //put your email address here 
         mail("[email protected]", $subject, $message, $from); 

         //prep json response 
         $ajaxresponse['type'] = 'success'; 
         $ajaxresponse['message'] = 'Thank You! Will be in touch soon'; 
       } catch(Exception $e){ 
         $ajaxresponse['type'] = 'error'; 
         $ajaxresponse['message'] = $e->getMessage(); 
       } 
       // now we are ready to turn this hash into JSON 
       print json_encode($ajaxresponse); 
       exit; 
     } 
?> 

那麼,使用表單發送電子郵件時是否存在安全問題?這個可以嗎? 謝謝!

+1

您可以考慮使用'filter_var'功能檢查電子郵件發送(和清理它),檢查發送的郵件是不是垃圾郵件,但在最壞的情況下,你可以不回答這個聯繫人 – Touki

+1

我記得在某處閱讀有關確保你添加一些檢查以確保沒有人注入cc和bcc字段放入郵件標題中。如果他們這樣做,那麼他們可以通過您的網絡表單發送電子郵件給他們希望的任何人。 – Ren

回答

3
  1. 您可以添加驗證碼以防止垃圾郵件。
  2. 您可以通過使用防止電子郵件注射:

    filter_var($電子郵件,FILTER_VALIDATE_EMAIL)

+0

究竟應該在哪裏添加這行代碼?在$ all_fields = array('name4','email4','message4')之後;可以嗎? –

+0

畢竟所有的領域都會很好。事實上,我會推薦它,因爲在嘗試驗證它之前,您需要確保您收到電子郵件POST字段。 –

+0

這會保護我免於向他人發送垃圾郵件?就像這裏的人們說的那樣。 –

0

在那裏我沒有看到安全問題,因爲您沒有修改服務器端的任何內容。可能是垃圾郵件的問題。添加一些驗證碼。其餘的看起來不錯。

+2

以及電子郵件注射如何? – haynar

1

我認爲這個表格是安全的,這意味着沒有人可以通過這種形式真正h @ ck您的網站。
但你需要添加一些更好的結果: 1.你也應該在php服務器端檢查post變量,這意味着你應該檢查電子郵件/名稱/消息是否有效
2.你應該添加一些captcha以防止垃圾郵件

6

一般來說,拇指的規則應始終爲:絕不信任用戶提供的數據。不,你的代碼不是防彈的。由於您不驗證和消毒用戶輸入,因此您同時使用mail()易受攻擊。用戶可以輕鬆地爲您提供email4提交的製作值。由於您直接使用表單數據,因此可以使用email4將其他郵件標頭注入發送郵件。它這些標題將是BCC:CC:甚至TO:然後,你將只是作爲垃圾郵件中繼。例如,如果我發佈這個

[email protected] 
CC: [email protected], [email protected], [email protected], 
X-Spam-Owned: Whoa 

爲您email4那麼你的頭會結束看起來像這樣:

To: [email protected] 
CC: [email protected], [email protected], [email protected], 
X-Spam-Owned: Whoa 

後你多數據簡單地膠文本與CRLFs。

爲了避免類似這樣的安全漏洞,您應該考慮放棄mail()並使用更巧妙的東西來處理類似的事情(不是mail()不好,但是您需要知道自己在做什麼,因爲它相當低比高級功能)。我建議使用PHPMailer或類似的軟件包。您應該始終驗證用戶提供的數據(特別是確保單行字段,如主題實際上是單行 - 剝離CRLF就足夠了)。當您打開自動錶單提交時添加驗證碼。

1

你還可以用

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { 
    /* special ajax here */ 
    die($content); 
} 

包裝你的服務器端代碼這將確保ajax請求正在服務器上。

請注意您的ID,您在您的問題中使用您的jQuery選擇器之一。

+0

感謝您的想法包裝它!我忘了那個ID :) –

1

即使您不使用數據庫,也可能存在電子郵件發送中的安全問題。當然,你不能用這種形式被黑,但是當用戶將輸入這樣的電子郵件領域存在的問題會occure:

[email protected] // there is a new line here 
CC:[email protected],[email protected],[email protected] 

,所以你能做的最好是消毒的所有郵件輸入字段功能,以防止垃圾郵件的傳遞。正如@ WebnetMobile.com早已傷心,從不信任用戶輸入

0

您應該添加驗證碼,客戶端和服務器端驗證形式