将文本区域接收的数据输出回文本区域时,如何正确清理从文本区域接收的数据?
用户将在文本区域中输入文本。 然后将其直接插入到 mySQL 数据库中。 我在上面使用了trim、htmlentities、mysql_real_escape_string,并且启用了魔术引号。 将数据输出回文本区域时应该如何清理它?
感谢您的帮助。 我从来都不太确定这样做的正确方法......
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
保存时不应使用
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 usehtmlentities
when displaying it. The rule of thumb is not to encode/sanitize the data until you need to. If you dohtmlentities
on it when you save then you have to dohtml_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 youmysql_real_escape_string
it. When displaying, you need to sanitize for XSS, so youhtmlentities
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.
除了 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 usemysql_real_escape_string()
.Other than that, I think you've got things covered.