清理用户提交内容的最佳方法?
可能的重复:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于不了解 PHP 或查找有关函数的文档的人:
>
和&
转换为 html 实体,从而防止 XSS。我真的不认为这里有必要解释。
For people who don't know PHP or find documentation about functions:
<
,>
and&
into html entities, thus, protecting against XSS.I really fail to see the need for explanation here.
您应该先对任何内容进行 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.
到目前为止,所有好的答案,我想补充一点,您应该确保输入数据采用所需的编码 - 您还应该规范化不同类型的新换行符或完全剥离控制字符,我最终使用以下内容功能很多:
它将从字符串中删除所有无效的 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:
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.