使用Ajax提交表单是否安全?

时间:2012-07-26 08:31:19

标签: php javascript ajax

所以我的表单是用html构建的,并在JS中验证,它看起来像我想要的那样。现在显然我将在服务器端使用PHP验证输入,但我想知道它是否足够安全,使用Ajax提交表单然后在服务器端验证,而不是使用'submit'类型提交表单按钮和'动作'属性。基本上,使服务器端验证依赖于JS提交它是否安全?

这是我的表格:

<form name="contactForm" id="contactForm"><!-- The form has no action attribute because its submitted via Ajax -->
<div id="inputsWrapper">
    <div>
        <label for="fullName">Your Name: <span class="required">(required)</span></label>
        <input type="text" name="fullName" id="fullName" title="First &amp; last name" value="First &amp; last name" maxlength="50" />
    </div>
    <div>
        <label for="email">Your E-mail: <span class="required">(required)</span></label>
        <input type="text" name="email" id="email" title="E-mail address" value="E-mail address" maxlength="500" />
    </div>
    <div>
        <label for="subject">In Regards To: <span class="required">(required)</span></label>
        <input type="text" name="subject" id="subject" title="Subject" value="Subject" maxlength="50"/>
    </div>
    <div>
        <label for="message">Your Message: <span class="required">(required)</span></label>
        <textarea name="message" id="message" title="Enter your message here" cols="40" rows="10">Enter your message here</textarea>
    </div>
</div> <!-- End inputsWrapper -->
<input type="button" name="sendBtn" id="sendBtn" value="Send Message" /><!-- This button has a listener assigned to it in JS and submits the form on click -->

单击该按钮后,Ajax将通过POST将表单提交到我的PHP脚本,并返回有效与否。这是否是一种安全的方式来做到这一点?谢谢你的任何建议。

2 个答案:

答案 0 :(得分:5)

通过Ajax或浏览器默认表单发布行为发布它不会从安全角度发挥作用。这些请求都是常规的HTTP POST / GET请求。

答案 1 :(得分:5)

  

我想知道使用Ajax提交表单然后在服务器端进行验证是否足够安全,而不是使用“提交”类型按钮和“操作”属性提交表单。

是。系统外部的输入是从系统外部输入的。

  

基本上,根据JS提交服务器端验证是否安全?

您的JavaScript应为unobtrusive并实施progressive enhancement

对于脚本的输入应该是相同的,无论它是来自Ajax的常规表单提交(如果使用jQuery's serialize之类的东西,这很容易),所以你不需要使服务器依赖于JS 进行响应。

表单提交和Ajax请求的处理方式的唯一区别应该是响应的格式化。

无论哪种方式,进入系统的数据最终都在提交者的控制之下,因此您需要进行适当的健全性检查并以任何方式逃避。