在 HTML 页面中显示纯 PHP 代码
我在这里谈论(特别是)论坛 - [PHP]代码[/PHP] - 风格。有些论坛会转义双引号或其他“危险字符”,而其他论坛则不会。
最好的方法是什么?你们都用什么? 可以做到不用担心代码注入吗?
编辑:谁说过要重新发明轮子?
And I'm talking (especially) forums here - [PHP]code here[/PHP] - style. Some forums escape double quotes or other "dangerous characters" and others don't.
What is the best method? What are you guys using?
Can it be done without the fear of code injection?
Edit: Who said anything about reinventing the wheel?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当 PHP
echo
或print
文本时,它永远不会执行它。只有eval
才会发生这种情况。这意味着如果您这样做:它将继续到页面输出并且不会被解析或执行。
这意味着您需要做的就是转义常用字符(
<
、>
、&
等),并且您应该一般来说是安全的。When PHP
echo
orprint
text, it never executes it. That only happens witheval
. This means that if you did this:it would carry through to the page output and not be parsed or executed.
This means that all you need to do is escape the usual characters (
<
,>
,&
, etc.) and you should generally be safe.不要重新发明轮子。我在你的问题中看到了 BBCode。获取一个 Markdown 库并使用它。所以使用这个: http://daringfireball.net/projects/markdown/
Don't reinvent the wheel. I see BBCode in your question. Grab a markdown library and use it instead. SO uses this: http://daringfireball.net/projects/markdown/
<
>
括号通常会被转义,原因显而易见。<
>
brackets usually being escaped, for obvious reason.如果您在某些页面上看到转义的引号,很可能是因为他们的脚本转义了两次(例如 magic_quotes 执行了一次,然后再次执行了 mysql_query() )。正确完成数据清理后,您不应在输出中看到转义字符。
If you see escaped quotes on some page, that's most likely because their script escaped them twice (for example magic_quotes did it once, then mysql_query() again). When data sanitisation is done properly, you should not see escape characters in output.