保护必须写入数据库的输入的步骤?

发布于 2024-10-09 15:37:42 字数 745 浏览 0 评论 0原文

假设我有一个 POST 文本区域的 Text 。在那个领域我应该可以写任何我想写的东西。例如 XSS、SQL 和其他最终的不良代码。对于标记,我将使用 BBCode,因此我想显示代码而不使其工作。
当然,我想避免的事情是数据库被搞砸了!有没有一种极其安全的方法来做到这一点?

我实际执行的步骤是:

  1. 指定 Content-Type 和字符集 Content-Type: text/html; charset=UTF-8

  2. 限制输入。 例如:最多 500 个字符

  3. 使用 htmlentities() $GoodText = htmlentities($Text, ENT_QUOTES, "UTF-8");

  4. 使用 mysql_real_escape_string() $GoodText = mysql_real_escape_string($GoodText);< /p>

  5. 将其存储在数据库中 $db->store($User, $GoodText); code>

据我所知,步骤 3 和 4 完全相同,但我'我想得到一个解释。

无论如何,这是一种工作方法吗?
我还应该对 POST 表单使用令牌吗?

Lets say I have Text that is a POST textarea. In that field I should be able to put whatever I'd like to write. Such as XSS, SQL, and other eventual bad codes. For the markup I'll use the BBCode, so I'd like to display the code without making it work.
The thing, of course, that I wanna avoid, is that the DB gets screwed! Is there an extremely safe way to do this?

The steps I actually do are:

  1. Specify the Content-Type and Charset Content-Type: text/html; charset=UTF-8

  2. Limit the input. Ex.: Max 500 characters

  3. Use htmlentities() $GoodText = htmlentities($Text, ENT_QUOTES, "UTF-8");

  4. Use mysql_real_escape_string() $GoodText = mysql_real_escape_string($GoodText);

  5. Store it in the DB $db->store($User, $GoodText);

As far as I read the step 3 and 4 are quite the same, but I'd like to get an explanation.

Anyway, is this a working method?
Should I also use tokens for POST forms?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放我走吧 2024-10-16 15:37:42

为您快速概述:

  • 针对跨站点请求使用令牌
    伪造。

  • 使用 htmlspecialchars/entities 来对抗
    跨站点脚本攻击。

  • 使用 mysql_real_escape_string 来对抗
    SQL注入。

为了数据库安全,您只需使用 mysql_real_escape_string 函数进行转义。

Quick overview for you:

  • Use tokens against cross site request
    forgeries.

  • Use htmlspecialchars/entities against
    cross site scripting attacks.

  • Use mysql_real_escape_string against
    SQL injection.

For database security, you'll only have to escape using the mysql_real_escape_string function.

硪扪都還晓 2024-10-16 15:37:42

我使用的这个函数基本上是步骤 3 和步骤 4 的结合。它适用于一维数组和字符串。

function escape($mixed){
    if(is_array($mixed)){
        foreach($mixed as $m => $value){
            $mixed[$m] = mysql_real_escape_string(htmlspecialchars($value, ENT_QUOTES, "UTF-8"));
        }
    }else{
        $mixed =  mysql_real_escape_string(htmlspecialchars($mixed, ENT_QUOTES, "UTF-8"));
    }
return $mixed;
}

I use this function that is basically steps 3 and 4 combined. It works with one dimensional array and strings.

function escape($mixed){
    if(is_array($mixed)){
        foreach($mixed as $m => $value){
            $mixed[$m] = mysql_real_escape_string(htmlspecialchars($value, ENT_QUOTES, "UTF-8"));
        }
    }else{
        $mixed =  mysql_real_escape_string(htmlspecialchars($mixed, ENT_QUOTES, "UTF-8"));
    }
return $mixed;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文