2012-09-07 71 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

您应该添加验证码,客户端和服务器端验证形式