清理用户提交内容的最佳方法?

发布于 2024-11-04 00:25:40 字数 472 浏览 1 评论 0原文

可能的重复:
PHP:终极清洁/安全功能

我正在开发一个实验性社交网络PHP 网站。所以,会有大量的用户提交的数据发送到数据库。

我不久前编写了一个自定义块脚本,它只会阻止提交某些字符或关键字。这有效,但它有一系列问题。

我听说addslashes和mysql_real_escape_string可以做到这一点,但在我得到一些可靠的建议之前我不想做任何事情。

我尝试了addslashes,它会在can't、don't等上添加斜杠。我不想要这样。

我只希望我的数据库免受 xss、html、php 和 javascript 攻击。有什么建议吗?

Possible Duplicate:
PHP: the ultimate clean/secure function

I am working on an experimental social networking site in PHP. So, there will be a lot of user submitted data sent to the database.

I had coded a custom block script a while back, that would just block certain characters or keywords from being submitted. This worked, but it had it's list of problems.

I heard addslashes and mysql_real_escape_string will do this, but I don't want to do anything until I get some solid advice.

I tried addslashes, and it will add slashes to can't, don't, etc. I don't want that.

I just want my database to be safe from xss, html, php, and javascript attacks. Any advice?

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

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

发布评论

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

评论(4

乜一 2024-11-11 00:25:41
  • 的准备语句
  • 来自 PDO filter_var() 函数
  • htmlspecialchars()

对于不了解 PHP 或查找有关函数的文档的人:

  • 准备好的语句 - 将提供针对 SQL 注入的保护(但不能防止极端愚蠢的
  • 行为 /code> 、 >& 转换为 html 实体,从而防止 XSS。

我真的不认为这里有必要解释。

  • prepared statements from PDO
  • filter_var() functions
  • htmlspecialchars()

For people who don't know PHP or find documentation about functions:

  • prepared statements - will provide protection against SQL injections ( but not against extreme stupidity )
  • filter_var() - will let you make sure that data really us an URL or email address , etc.
  • htmlspecialchars() - converts characters like < , > and & into html entities, thus, protecting against XSS.

I really fail to see the need for explanation here.

樱娆 2024-11-11 00:25:41

您应该先对任何内容进行 HTML 转义,然后再将其输出回用户。然后当它被输出回来时它就会安全了。对 PHP 使用 htmlspecialchars。请参阅最佳实践是什么避免 PHP 站点中的 xss 攻击了解更多信息并阅读 OWASP XSS(跨站脚本)预防备忘单

You should HTML escape any content before outputting it back to the user. Then when it's ever outputted back it'll be safe. Use htmlspecialchars for PHP. See What are the best practices for avoiding xss attacks in a PHP site for more information and read OWASP XSS (Cross Site Scripting) Prevention Cheat Sheet.

满地尘埃落定 2024-11-11 00:25:41

到目前为止,所有好的答案,我想补充一点,您应该确保输入数据采用所需的编码 - 您还应该规范化不同类型的新换行符或完全剥离控制字符,我最终使用以下内容功能很多:

function Filter($string, $control = true)
{
    $string = iconv('UTF-8', 'UTF-8//IGNORE', $string);

    if ($control === true)
    {
        return preg_replace('~\p{C}+~u', '', $string);
    }

    return preg_replace(array('~\r[\n]?~', '~[^\P{C}\t\n]+~u'), array("\n", ''), $string);
}

它将从字符串中删除所有无效的 UTF-8 数据并规范新行。所有控制字符(制表符 (\t) 和换行符 (\n) 除外)都会被条带化,并且如果 $control == true 这些字符也被剥夺了。


PS:从安全角度来看,这不是很有用,但有助于避免 吉戈

All good answers so far, I would just like to add that you should make sure that the input data comes in the desired encoding - you should also normalize the different types of new line feeds or strip control characters altogether, I end up using the following function a lot:

function Filter($string, $control = true)
{
    $string = iconv('UTF-8', 'UTF-8//IGNORE', $string);

    if ($control === true)
    {
        return preg_replace('~\p{C}+~u', '', $string);
    }

    return preg_replace(array('~\r[\n]?~', '~[^\P{C}\t\n]+~u'), array("\n", ''), $string);
}

It will remove all invalid UTF-8 data from the string and normalize new lines. All control chars (except tab (\t) and new lines (\n)) are striped, and if $control == true these are stripped too.


PS: This is not very useful from a security standpoint of view but is helps avoiding GIGO.

金橙橙 2024-11-11 00:25:41
  1. 对于 HTML 类型输入,请使用 HTMLPurifier 或类似工具过滤掉不需要的标记。
  2. 存储数据之前验证表单字段
  3. 写入数据库时​​,将准备好的语句与 PDO 或 MySQLi 一起使用。如果您正确绑定参数,这将为您处理 SQL 转义问题。
  4. 在显示之前对来自数据库的输出进行转义,除非它被认为是安全的。
  1. For HTML type input use HTMLPurifier or similar to filter out unwanted markup.
  2. Validate form fields before storing the data
  3. Use prepared statements with PDO or MySQLi when writing to the database. This will take care of the SQL escaping for you, provided you bind your parameters correctly.
  4. Escape the output coming out of the DB before displaying it unless it can be considered safe.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文