为什么打开 magic_quotes_gpc 被认为是一种不好的做法?

发布于 2024-08-27 23:33:23 字数 68 浏览 6 评论 0原文

为什么在 PHP 中打开 ma​​gic_quotes_gpc 被认为是一种不好的做法?

Why is turning on magic_quotes_gpc in PHP considered a bad practice?

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

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

发布评论

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

评论(6

伴梦长久 2024-09-03 23:33:23

我认为我无法比 PHP 本身的制造者更好地解释它(在该页面上有后续评论): 为什么不使用 Magic Quotes

  • 可移植性:假设它打开或关闭,都会影响可移植性。使用 get_magic_quotes_gpc() 来检查这一点,并进行相应的编码。
  • 性能:由于并非所有转义数据都会插入数据库,因此转义所有这些数据会造成性能损失。在运行时简单地调用转义函数(例如 addslashes())会更有效。尽管 php.ini-development 默认启用这些指令,但 php.ini-product 会禁用它。此建议主要是出于性能原因。
  • 不便:因为并非所有数据都需要转义,所以在不应该出现的地方看到转义的数据通常会很烦人。例如,从表单发送电子邮件,并在电子邮件中看到一堆 \'。要解决此问题,可能需要过度使用 stripslashes()

注意 - 从 PHP 5.3.0 开始,此功能已被弃用,并从 PHP 5.4.0 开始被删除。

I don't think I can explain it any better than the makers of PHP itself (with followup comments on that page): Why not to use Magic Quotes

  • Portability: Assuming it to be on, or off, affects portability. Use get_magic_quotes_gpc() to check for this, and code accordingly.
  • Performance: Because not every piece of escaped data is inserted into a database, there is a performance loss for escaping all this data. Simply calling on the escaping functions (like addslashes()) at runtime is more efficient. Although php.ini-development enables these directives by default, php.ini-production disables it. This recommendation is mainly due to performance reasons.
  • Inconvenience: Because not all data needs escaping, it's often annoying to see escaped data where it shouldn't be. For example, emailing from a form, and seeing a bunch of \' within the email. To fix, this may require excessive use of stripslashes().

Note - This feature has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0.

吾家有女初长成 2024-09-03 23:33:23

根据文章 PHP 和 php.ini 中的 Magic Quotes GPC (magic_quotes_gpc) 是什么? ,有很多缺点: 将

  • 表单提交发送回浏览器的情况必须通过调用 stripslashes() 手动删除斜杠。
  • 如果此服务器的魔术引号被关闭,或者代码被移动到未启用魔术引号的服务器,您的脚本将失败。或者更糟糕的是,不会立即失败而只会表现出奇怪的行为。
  • 对提交的变量进行的任何字符串操作,甚至是简单的“if”语句,都必须考虑内容中斜杠扭曲的可能性。
  • 神奇的引用会导致开发人员的草率。转义插入 SQL 查询中的变量(在我看来)是开发人员应该了解和考虑的事情。不只是假设一切都是花花公子。

According to the article What is Magic Quotes GPC (magic_quotes_gpc) in PHP and the php.ini?, there are many disadvantages:

  • Cases where form submissions are sent back to the browser must have the slashes removed manually with a call to stripslashes().
  • If magic quotes are ever turned off for this server, or the code is moved to a server where magic quotes isn't enabled your scripts will fail. Or worse, not fail immediately and only exhibit strange behaviour.
  • Any string operations on submitted variables, even simple 'if' statements must take into account the possiblity of slashes warping in the content.
  • Magic quotes breeds developer sloppyness. Escaping variables inserted into an SQL query (in my opinion) is something that a developer should know and think about. Not just assume everything is dandy.
清欢 2024-09-03 23:33:23

因为有人可以将您的脚本移动到未启用该选项的服务器,从而立即在您的应用程序中打开数百个安全漏洞。此外,太多人认为启用魔术引号可以使您的应用程序安全。事实并非如此。您仍然需要检查和验证应用程序中的每一条输入。即使您没有引用问题,您仍然可能会遇到跨站点脚本问题等。

尽管该功能在 PHP 的未来版本中已被删除。

Because somebody can move your script to a server where that option is not enabled, instantly opening hundreds of security holes in your application. Also, too many think that enabling magic quotes makes your application secure. It does not. You still need to examine and verify every piece of input that comes into your application. Even if you don't have quote problems, you can still have cross site scripting issues, etc.

The fact that the feature is being removed in future versions of PHP notwithstanding.

随梦而飞# 2024-09-03 23:33:23

因为关闭它会迫使您编写更安全的代码。

如果奥马利先生去你的网站注册,那么 magic_quotes_gpc 就会把他的姓氏变成 O\'Malley,当你将其插入数据库时​​,一切都会顺利。

问题是,magic_quotes 来自addslashes - 这并不一定可以作为数据库系统的转义。 O'Malley 可能会起作用,但也有可能绕过这种转义并进行 SQL 注入。

如果 magic_quotes 没有打开,那么您将得到字符串 O'Malley,并且它将破坏 SQL 语句,例如

INSERT INTO users (...) VALUES (...,'O'Malley',...)

“注意字符串在 O 之后确实终止”。

此外,它更好:例如,如果您要发送一封带有他名字的电子邮件,你必须去掉斜杠——没有充分的理由。如果您不这样做,您将收到一封来自奥马利先生的电子邮件。

(当然,对于真正安全的数据库处理代码,您需要使用参数化查询,因为这是防止 SQL 注入的最佳方法。如果您参数化,您无论如何都不需要斜杠,而且这是浪费是时候让 PHP 添加它们了。)

Because leaving it off forces you to write more secure code.

If Mr. O'Malley goes to register on your site, then magic_quotes_gpc will turn his last name into O\'Malley, and when you insert it into the database, everything will go fine.

The problem is, the magic_quotes come from addslashes - which doesn't necessarily work as escaping for your database system. O'Malley might work, but it may also be possible to circumvent this escaping and do SQL injection.

If magic_quotes aren't on, then you'll get the string O'Malley, and it will break an SQL statement like

INSERT INTO users (...) VALUES (...,'O'Malley',...)

Notice the string is really terminated after the O.

Additionally, it's nicer: if you were to, for example, send an e-mail with his name, you'd have to stripslashes - for no good reason. If you don't you'll get an e-mail from Mr. O\'Malley.

(Of course, for REALLY secure database handling code, you'd want to use parameterized queries, since that is the best way to prevent SQL injection. And if you parameterize, you don't want the slashes anyway, and it's a waste of time to have PHP add them.)

海之角 2024-09-03 23:33:23

“Magic Quotes”是 PHP 的一次尝试,防止开发人员在不了解情况时用 SQL 注入搬起石头砸自己的脚。它在 PHP 5.3 中已被弃用,并将在 PHP 6 中被删除。

我认为最好是显式地转义需要转义的内容,而不是转义所有内容并且必须转义永远不会放置的内容在数据库中。魔法引言制造的问题比它解决的问题多(或更多),试图保护那些应该更了解的人。

https://www.php.net/manual/en/security.magicquotes。 php

"Magic Quotes" was PHP's attempt at hand holding, preventing developers from shooting themselves in the foot with SQL injection when they didn't know any better. It's deprecated in PHP 5.3, and will be removed in PHP 6.

I say it's better to be explicit and escape what needs to be escaped, rather than escape everything and have to unescape things that will never be placed in the database. Magic quotes creates as many (or more) problems than it solves, in an attempt to shield people who should know better.

https://www.php.net/manual/en/security.magicquotes.php

陈独秀 2024-09-03 23:33:23

非常简单的问题。
想象一下您想通过电子邮件发送用户数据。或者将 cookie 中的用户名插入到表单输入中。你认为像鲍勃·“布法罗”·比尔这样的名字是个好主意吗?我不这么认为

Very easy question.
Imagine you want to send user's data via email. Or insert user's name from cookie into the form input. Do you think it will be good idea to have such names like Bob \"Buffalo\" Bill? I don't think so

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