为什么打开 magic_quotes_gpc 被认为是一种不好的做法?
为什么在 PHP 中打开 magic_quotes_gpc 被认为是一种不好的做法?
Why is turning on magic_quotes_gpc in PHP considered a bad practice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为我无法比 PHP 本身的制造者更好地解释它(在该页面上有后续评论): 为什么不使用 Magic Quotes
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
get_magic_quotes_gpc()
to check for this, and code accordingly.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.stripslashes()
.Note - This feature has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0.
根据文章 PHP 和 php.ini 中的 Magic Quotes GPC (magic_quotes_gpc) 是什么? ,有很多缺点: 将
According to the article What is Magic Quotes GPC (magic_quotes_gpc) in PHP and the php.ini?, there are many disadvantages:
因为有人可以将您的脚本移动到未启用该选项的服务器,从而立即在您的应用程序中打开数百个安全漏洞。此外,太多人认为启用魔术引号可以使您的应用程序安全。事实并非如此。您仍然需要检查和验证应用程序中的每一条输入。即使您没有引用问题,您仍然可能会遇到跨站点脚本问题等。
尽管该功能在 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.
因为关闭它会迫使您编写更安全的代码。
如果奥马利先生去你的网站注册,那么 magic_quotes_gpc 就会把他的姓氏变成 O\'Malley,当你将其插入数据库时,一切都会顺利。
问题是,magic_quotes 来自addslashes - 这并不一定可以作为数据库系统的转义。 O'Malley 可能会起作用,但也有可能绕过这种转义并进行 SQL 注入。
如果 magic_quotes 没有打开,那么您将得到字符串 O'Malley,并且它将破坏 SQL 语句,例如
“注意字符串在 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
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.)
“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
非常简单的问题。
想象一下您想通过电子邮件发送用户数据。或者将 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