简体   繁体   English

PHP将PHP回显到浏览器,没有显示出来,安全问题?

[英]php echo php to browser, not showing up, security issue?

What happen if i use the following? 如果我使用以下内容会怎样?

<?php echo "<?php echo date('Y'); ?>"; ?>

i could not find an answer anywhere, and when i try it myself, i get: 我在任何地方都找不到答案,当我自己尝试时,我得到:

<?php echo date('Y'); ?></td></tr></table>

However, it does not show up on front browser, only source. 但是,它不会显示在前端浏览器中,只能显示在源代码中。

So my question is, does this affect the html/browser/server in any way? 所以我的问题是,这是否会以任何方式影响html / browser / server? as i do not want to end up creating a security issue should user post their own php code in a html only format, like a bio page etc. 因为我不想最终导致安全问题,所以用户应该以html格式发布自己的php代码,例如生物页面等。

It's because of the chevrons ('<' and '>'), because the browser interprets them as tags. 这是因为人字形(“ <”和“>”),因为浏览器将它们解释为标签。

There are 2 ways you could get round this. 有两种方法可以解决此问题。

Either use the codes for special characters, so you would do: 可以使用特殊字符代码,因此您可以执行以下操作:

<?php echo "&#60;?php echo date('Y'); ?&#62;"; ?>

Or, an easier way, use the htmlspecialchars() function: 或者,更简单的方法是使用htmlspecialchars()函数:

<?php echo htmlspecialchars("<?php echo 'hi'; ?>"); ?>

More info on the htmlspecialchars() function can be found at http://www.php.net//manual/en/function.htmlspecialchars.php 有关htmlspecialchars()函数的更多信息,请参见http://www.php.net//manual/en/function.htmlspecialchars.php

It is not a security problem and will not have any effects on browser or server, at least not because of PHP code. 这不是安全问题,并且至少不会由于PHP代码而对浏览器或服务器没有任何影响。 Even if the string contains PHP code it will just be sent to the client which will not attempt to execute it. 即使该字符串包含PHP代码,也只会将其发送到不会尝试执行它的客户端。

The real problem when echo ing user-defined HTML is the risk of attacks such as XSS. echo用户定义的HTML时,真正的问题是存在诸如XSS之类的攻击风险。 Users could include arbitrary scripts or images or scramble the rest of the page by inserting arbitrary tags. 用户可以包括任意脚本或图像,也可以通过插入任意标签来扰乱页面的其余部分。 In other words: Users could modify the whole page with a single line of HTML. 换句话说:用户可以用一行HTML修改整个页面。

In general, it's a bad practice to allow such arbitrary input. 通常,允许这样的任意输入是不好的做法。 Have a look at strip_tags which provides a very basic level of protection. 看一下strip_tags ,它提供了非常基本的保护级别。

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

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