我正在建立一个小型论坛,当我对其发表评论时遇到了这个问题。其中有 <
,然后 htmlspecialchars 将其转换为 >
。 链接到论坛(它是丹麦语,但你应该能够使用它)。
im building a small forum and got this problem that when i post a comment on it. That have <
inside it then htmlspecialchars is converting it to >
. link to forum (it's danish but your should be able to use it).
发布评论
评论(1)
如果您想在注释中允许使用特殊字符(在我看来,对于像“注释”这样的通用字段来说这是正确的做法),那么您应该只在写入字段内容时担心特殊字符 进入一些潜在的“危险”上下文:SQL 代码、HTML、JavaScript,甚至日志文件。每一种情况都有其自身的危险,因此“引用”因情况而异。
如果您想向用户显示评论时应用 HTML 的特殊字符过滤,则“<”的转换
>
正是您想要的。当浏览器将注释文本解析为 HTML 页面的一部分时,>
将转换为“<”屏幕上的字形。如果您在阅读评论和编写评论时执行转换,最终会遇到双重编码错误:“&”
>
中的字符本身将转换为&
。因此,浏览器将看到字符串>
,这将导致它在屏幕上显示>
。If you want to allow special characters in the comments (which, in my opinion, is the right thing to do for a general field like "Comments"), then you should only worry about special characters when the field contents are written into some potentially "dangerous" context: SQL code, HTML, JavaScript, or even log files. Each of those contexts has its own hazards, so "quoting" varies from one to the next.
If you apply the special characters filtering for HTML when you want to show the comments to the user, then that conversion of "<" to
>
is exactly what you want. When the browser parses the comment text as part of your HTML page, the>
will be transformed into the "<" glyph on the screen.If you perform the transformation when you read the comment and when you write it, you'll end up with a double-encoding bug: the "&" character in
>
will itself be transformed into&
. The browser will therefore see the string>
which will cause it to show>
on the screen instead.