PHP 保护自己免受 SQL 注入?
当我将 ");--
从输入字段发送到我的本地 PHP 服务器时,它会自动将其转换为
\");--
看起来很棒,只是我不知道这种行为有多值得信赖。 虽然看起来是避免了SQL注入,但是我的开发环境和生产环境不一样,恐怕生产环境可能没有自动激活这种保护……
为什么PHP要这样做(不转换输入)必须使用 mysql_real_escape_string )? 它总是这样做还是只使用某些扩展? 依靠这种行为来防止 SQL 注入安全吗?
When I send ");--
from an input field to my localhost PHP server, it AUTOMATICALLY converts it to
\");--
It seems great, except that I don't know how trustworthy this behavior is.
Although it seems to avoid SQL injections, my development environment is not the same as the production environment and I'm afraid that the production environment may not have this sort of protection automatically activated...
Why does PHP does this(convert the input without having to use mysql_real_escape_string
)? Does it always do it or only with certain extensions? Is it safe to rely on this behavior to prevent SQL injections?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您似乎已启用Magic Quotes。 但您最好禁用此选项或恢复它们。
mysql_real_escape_string
更安全。It seems that you have Magic Quotes enabled. But you better disable this option or revert them.
mysql_real_escape_string
is more secure.PHP 的这一“功能”被称为“魔术引号”。 尽管它们可能很“神奇”,但使用它们是极其糟糕的做法,因为它们只会给人一种错误的安全感。 值得庆幸的是,它们已从 PHP 6(正在开发中)中删除。
更详细的批评列表可以在这篇维基百科文章中找到。
PHP 手册描述了禁用魔术引号的各种方法。
This "feature" of PHP is known as "magic quotes". As 'magic' as they may be, it is extremely bad practice to use them, as they do little more than give a false sense of security. Thankfully they have been removed from PHP 6 (in development).
A more detailed list of criticisms can be found in this Wikipedia article.
The PHP manual describes various ways to disable magic quotes.
您可能想使用抽象层与数据库进行对话,例如 Zend_Db。 例如,如果您通过实例化 Zend_Db_Select 来创建 select 语句,它将如下所示:
You might want to get into talking to the database using an abstraction layer like Zend_Db. For example, if you create a select statement by instantiating a Zend_Db_Select, it would look like this:
您已打开“魔法报价”。 PHP 小组正式强烈反对此函数,并强烈建议不要依赖它。 无论您在脚本中使用
.htaccess
或ini_set()
,在运行时禁用魔术引号的方法并不总是有效。 一直调用 stripslashes 也会变得非常混乱。更多详细信息:https://www.php.net/magic_quotes
You have Magic Quotes turned on. The PHP group officially deprecated this function strongly, and strongly discourages relying on it. Ways to disable magic quotes at runtime don't always work, weather you use
.htaccess
orini_set()
in the script. Calling stripslashes all the time can also become pretty messy.More details: https://www.php.net/magic_quotes