在 php 中处理用户输入数据。什么更好?

发布于 2024-08-13 11:23:11 字数 236 浏览 9 评论 0原文

我试图找出管理用户输入的有关他可能插入的不需要的标签的数据的最佳方法是什么:

  • strip_tags() - 标签被删除,并且不会插入数据库
  • 中标签被插入到数据库中,但是当读取该字段并将其显示给用户时,我们将使用 htmlspecialchars()

有什么更好的方法,这些方法有什么缺点吗?

问候

I am trying to figure out what is the best way to manage the data a user inputs concerning non desirable tags he might insert:

  • strip_tags() - the tags are removed and they are not inserted in the database
  • the tags are inserted in the database, but when reading that field and displaying it to the user we would use htmlspecialchars()

What's the better, and is there any disadvantage in any of these?

Regards

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

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

发布评论

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

评论(3

北斗星光 2024-08-20 11:23:11

这取决于您的优先级:

  • 如果显示用户输入中的特殊字符很重要(例如在 StackOverflow 上),那么您需要将此信息存储在数据库中并在显示时对其进行清理 - 在这种情况下,您至少需要使用 htmlspecialchars() 来显示输出(如果不是更复杂的东西),
  • 如果您只想纯文本注释,请在坚持之前使用 strip_tags()它在数据库中 - 这样您将减少需要存储的数据量,并减少在屏幕上显示数据时的处理时间

This depends on what your priority is:

  • if it's important to display special characters from user input (like on StackOverflow, for example), then you'll need to store this information in the database and sanitize it on display - in this case, you'll want to at least use htmlspecialchars() to display the output (if not something more sophisticated)
  • if you just want plain text comments, use strip_tags() before you stick it in the database - this way you'll reduce the amount of data that you need to store, and reduce processing time when displaying the data on the screen
苍风燃霜 2024-08-20 11:23:11

标签被插入到数据库中,但是当读取该字段并将其显示给用户时,我们将使用 htmlspecialchars()

这个。您通常希望人们能够输入小于号和与号并将它们显示在页面上。在每个文本到 HTML 输出步骤(无论该文本直接来自用户输入,还是来自数据库,或者完全来自其他地方)上的 htmlspecialchars 是实现此目的的正确方法。对于处理输出编码问题来说,弄乱输入是一种完全不合适的策略。

当然,您需要不同的转义(或参数化)来将文本放入 SQL 字符串中。

the tags are inserted in the database, but when reading that field and displaying it to the user we would use htmlspecialchars()

This. You usually want people to be able to type less-than signs and ampersands and have them displayed as such on the page. htmlspecialchars on every text-to-HTML output step (whether that text came directly from user input, or from the database, or from somewhere else entirely) is the right way to achieve this. Messing about with the input is a not-at-all-appropriate tactic for dealing with an output-encoding issue.

Of course, you will need a different escape — or parameterisation — for putting text in an SQL string.

你的笑 2024-08-20 11:23:11

为保护用户输入而采取的措施完全取决于数据的使用环境。例如:

  • 如果要将其插入 SQL 数据库,则应该使用参数化语句。 PHP 的 mysql_real_escape_string() 也能正常工作。
  • 如果要在 HTML 页面上显示它,则需要剥离或转义 HTML 标记。
  • 一般来说,每当您将用户输入与另一种形式的标记或另一种语言混合时,该语言的元素都需要在放入该上下文之前从输入中转义或剥离。

上面的最后一点延续到下一点:许多人认为应该始终保留原始输入。当您稍后决定以不同的方式使用数据时,这很有意义,例如,HTML 标记在新上下文中并不是什么大问题。此外,如果您的网站以某种方式受到损害,您还会记录所给的确切输入。

特别与用于在 HTML 页面上显示的用户输入中的 HTML 标记相关:如果用户输入 HTML 标记有任何可能的原因,则只需转义它们即可。如果没有,请在显示前将其剥离。

The measures taken to secure user input depends entirely on in what context the data is being used. For instance:

  • If you're inserting it into a SQL database, you should use parameterized statements. PHP's mysql_real_escape_string() works decently, as well.
  • If you're going to display it on an HTML page, then you need to strip or escape HTML tags.
  • In general, any time you're mixing user input with another form of mark-up or another language, that language's elements need to be escaped or stripped from the input before put into that context.

The last point above segues into the next point: Many feel that the original input should always be maintained. This makes a lot of sense when, later, you decide to use the data in a different way and, for instance, HTML tags aren't a big deal in the new context. Also, if your site is in some way compromised, you have a record of the exact input given.

Specifically related to HTML tags in user input intended for display on an HTML page: If there is any conceivable reason for a user to input HTML tags, then simply escape them. If not, strip them before display.

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