防止表单中的 php 注入(如果用户将 php 代码提交到表单中)

发布于 2024-11-04 05:40:48 字数 204 浏览 3 评论 0原文

在联系表单中,如果有人在文本框中输入以下内容:

<?php echo 'hi'; ?>

我看到服务器由于错误而不会执行它。相反,我希望它以某种方式将其转义为纯文本并正确显示。我见过其他网站能够做到这一点。我原本以为这个问题可以通过addslashes()函数来解决,但这似乎不起作用。

谢谢, 菲尔

In a contact form, if someone enters the following into the textbox:

<?php echo 'hi'; ?>

I see that the server will not execute it because of an error. What I would like it to do is instead, somehow escape it into plain text and display it correctly. I have seen other sites been able to do this. I originally thought this could be solved by the addslashes() function, but that doesn't seem to work.

Thanks,
Phil

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

朦胧时间 2024-11-11 05:40:49

不可以。请使用 htmlspecialchars 代替。不要使用 addslashes

更具体地说,addslashes 直接转义 '"\ 以及 NUL< 的所有实例。 /code>。它的目的是防止SQL注入,但它在适当的安全性方面没有真正的用处。 。

你想要的是阻止浏览器 按原样解释标签(这与防止 SQL 注入完全不同),例如,如果我想谈论

在您的情况下,htmlspecialchars 之后的输出将如下所示:

<?php echo 'hi'; ?>

No. Use htmlspecialchars instead. Don't use addslashes.

To be more specific, addslashes bluntly escapes all instances of ', " and \ and NUL. It was meant to prevent SQL injection, but it has no real use in proper security measures.

What you want is preventing the browser to interpret tags as is (and that's entirely different from preventing SQL injections). For instance, if I want to talk about <script> elements, SO shouldn't simply send that string literally, causing to start an actual script (that can lead to Cross-site scripting), but some characters, especially < and >, need to be encoded as HTML entities so they're shown as angle brackets (the same is true for &, that otherwise would be interpreted as the start of an HTML entity).

In your case, output after htmlspecialchars would look like:

<?php echo 'hi'; ?>
帅哥哥的热头脑 2024-11-11 05:40:49

在输出用户提供的任何内容之前使用 htmlspecialchars 。但在这种情况下,还要确保您执行用户输入的任何内容。不要使用 eval、include 或 require。如果将用户数据保存到文件中,请使用 readfilefile_get_contents+htmlspecialchars 而不是 include/require。如果你使用的是eval,请将其更改为echo等。

Use htmlspecialchars before outputing anything provided by the user. But in this case, also make sure that you do not execute anything the user inputs. Do not use eval, include or require. If you save the user data to a file, use readfile or file_get_contents+htmlspecialchars instead of include/require. If you're using eval, change it into echo and so on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文