最安全的方法来处理POST操作

时间:2013-07-23 10:40:29

标签: php html forms

这是我目前处理POST提交操作的方式:

<form action="process.php" method="post">
//several form fields 
<input type="submit" name="ok" value="save" />

然后在处理页面上我有这样的东西:

if(isset($_POST['ok']) && $_POST['ok']=="save")
{
    //process the action, and possibly save to database
}

现在我担心恶意的人可能会这样做(来自他们网站上的脚本)

<form action="http://www.mysite.com/process.php" method="post">
//he can "view source" on my site, view the fields i'm having and put them
//then put the submit button
<input type="submit" name="ok" value="save" />
当然,你知道我会喝点热汤。 那么我该怎么做,或者处理和处理POST提交动作最安全的方法是什么?

2 个答案:

答案 0 :(得分:4)

这里有两个潜在的问题。

阻止Mallory向Bob的网站发出恶意请求

  1. 对用户进行身份验证(使用OAuth,用户名和密码等)
  2. 检查经过身份验证的用户是否已获得授权,以便在继续执行之前执行请求。
  3. 阻止Mallory欺骗Alice向Bob的网站发出恶意请求

    这是CSRF attackDefend against与否。{{3}}

答案 1 :(得分:2)

您正在以错误的级别查看问题。当然,您应首先确保发出请求的人(您有会话,对吗?)具有所需的权限。 HTML表单中没有任何内容阻止他们执行您描述的内容,因此您需要确保提交表单的人实际上可以这样做。

(从技术上讲,你可以检查推荐人,但这最好是脆弱的。)