将文本区域接收的数据输出回文本区域时,如何正确清理从文本区域接收的数据?

发布于 2024-07-14 02:42:50 字数 173 浏览 9 评论 0 原文

用户将在文本区域中输入文本。 然后将其直接插入到 mySQL 数据库中。 我在上面使用了trim、htmlentities、mysql_real_escape_string,并且启用了魔术引号。 将数据输出回文本区域时应该如何清理它?

感谢您的帮助。 我从来都不太确定这样做的正确方法......

A user will input text in a textarea. It is then inserted directly into a mySQL database. I use trim, htmlentities, mysql_real_escape_string on it and I have magic quotes enabled. How should I sanitize it when outputting that data back into a textarea?

Thanks for your help. I've never been too sure on the correct way of doing this...

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

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

发布评论

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

评论(2

擦肩而过的背影 2024-07-21 02:42:50

保存时不应使用 htmlentities。 显示它时应该使用htmlentities。 经验法则是,除非需要,否则不要对数据进行编码/清理。 如果您在保存时对其执行 htmlentities 操作,那么当用户想要编辑输入时,您必须对文本执行 html_entity_decode 操作。 所以你只需要消毒你需要的东西,仅此而已。 保存它时,您需要清理 SQL 注入,因此您 mysql_real_escape_string 它。 显示时,您需要清理 XSS,因此您需要 htmlentities 它。

另外,我不确定您是否看到了 Darryl Hein 的评论,但您确实不希望启用 magic_quotes。 它们是一件很糟糕的事情,并且从 PHP 5.3 开始已被弃用,并将PHP 6 中完全消失了。

You shouldn't use htmlentities when saving it. You should use htmlentities when displaying it. The rule of thumb is not to encode/sanitize the data until you need to. If you do htmlentities on it when you save then you have to do html_entity_decode on the text when the user wants to edit the input. So you sanitize for what you need and nothing more. When saving it, you need to sanitize for SQL injection, so you mysql_real_escape_string it. When displaying, you need to sanitize for XSS, so you htmlentities it.

Also, I am not sure if you saw Darryl Hein's comment, but you really do not want magic_quotes enabled. They are a bad, bad, thing and have been deprecated as of PHP 5.3 and will be gone altogether in PHP 6.

似狗非友 2024-07-21 02:42:50

除了 Paolo 关于何时使用 htmlentities() 的回答之外,除非您使用旧版本的 PHP,否则清理插入 mysql DB 的正确方法是使用 准备好的语句mysqli 扩展。 这取代了使用 mysql_real_escape_string() 的任何需要。

除此之外,我认为你已经涵盖了一些事情。

In addition to Paolo's answer about when to use htmlentities(), unless you're using an old version of PHP, the correct way to sanitize for insertion into a mysql DB is to use Prepared Statements which are part of the mysqli extension. This replaces any need to use mysql_real_escape_string().

Other than that, I think you've got things covered.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文