简体   繁体   English

在HTML页面中显示纯PHP代码

[英]Showing plain PHP code in a HTML page

And I'm talking (especially) forums here - [PHP]code here[/PHP] - style . 我在这里(特别是)论坛 - [PHP]代码[/ PHP] - 风格 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? 编辑:谁说重新发明轮子?

When PHP echo or print text, it never executes it. 当PHP echoprint文本时,它永远不会执行它。 That only happens with eval . 这只发生在eval This means that if you did this: 这意味着如果你这样做:

echo '<?php ... ?>';

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. 这意味着你需要做的只是逃避通常的字符( <>&等),你通常应该是安全的。

Don't reinvent the wheel. 不要重新发明轮子。 I see BBCode in your question. 我在你的问题中看到了BBCode。 Grab a markdown library and use it instead. 拿一个降价库并改为使用它。 SO uses this: http://daringfireball.net/projects/markdown/ SO使用这个: http//daringfireball.net/projects/markdown/

  1. There is no fear of PHP code injection (unless you are doing some unusual things like eval'ing HTML templates) but always a fear of JS code injection, often called XSS. 不用担心PHP代码注入(除非你正在做一些不寻常的事情,比如评估HTML模板),但总是担心JS代码注入,通常称为XSS。 And all danger coming only from possible JS code. 所有危险都来自可能的JS代码。
  2. Thus, there is no special treatment for the PHP code, shown on a HTML page. 因此,HTML页面上显示的PHP代码没有特殊处理。 Just treat it as any other data. 只需将其视为任何其他数据。 < > brackets usually being escaped, for obvious reason. < >括号通常被转义,原因很明显。
  3. Don't reinvent the wheel. 不要重新发明轮子。 PHP has it's highlight_string function for this PHP有它的highlight_string函数

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). 如果您在某些页面上看到转义引号,那很可能是因为它们的脚本将它们转义两次(例如,magic_quotes只执行一次,然后再次使用mysql_query())。 When data sanitisation is done properly, you should not see escape characters in output. 正确完成数据清理后,您不应在输出中看到转义字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM