PHP 保护自己免受 SQL 注入?

发布于 2024-07-15 18:27:47 字数 305 浏览 4 评论 0原文

当我将 ");-- 从输入字段发送到我的本地 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 技术交流群。

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

发布评论

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

评论(4

段念尘 2024-07-22 18:27:47

您似乎已启用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.

如此安好 2024-07-22 18:27:47

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.

温柔嚣张 2024-07-22 18:27:47

您可能想使用抽象层与数据库进行对话,例如 Zend_Db。 例如,如果您通过实例化 Zend_Db_Select 来创建 select 语句,它将如下所示:

//$_GET['thing'] is automatically escaped 
$select = $zdb->select()->from('things')->where('name = ?',$_GET['thing']);
$result = $zdb->fetchRow($select->__toString());//__toString generates a really pretty, vendor independent query

//a plain vanilla query would look like this:
$result = $zdb->fetchRow('select * from things where name = ?', $zdb->quote($_GET['thing']);

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:

//$_GET['thing'] is automatically escaped 
$select = $zdb->select()->from('things')->where('name = ?',$_GET['thing']);
$result = $zdb->fetchRow($select->__toString());//__toString generates a really pretty, vendor independent query

//a plain vanilla query would look like this:
$result = $zdb->fetchRow('select * from things where name = ?', $zdb->quote($_GET['thing']);
半仙 2024-07-22 18:27:47

您已打开“魔法报价”。 PHP 小组正式强烈反对此函数,并强烈建议不要依赖它。 无论您在脚本中使用 .htaccessini_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 or ini_set() in the script. Calling stripslashes all the time can also become pretty messy.

More details: https://www.php.net/magic_quotes

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