【发布时间】:2010-01-04 01:11:44
【问题描述】:
我见过几十个这样的 PHP sn-ps:
function DB_Quote($string)
{
if (get_magic_quotes_gpc() == true)
{
$string = stripslashes($string);
}
return mysql_real_escape_string($string);
}
如果我拨打DB_Quote("the (\\) character is cool"); 会发生什么? (感谢 jspcal!)
难道我们不应该只在get_magic_quotes_gpc() == true 并且值源自$_GET、$_POST 或$_COOKIE superglobals 时才去除斜线吗?
【问题讨论】:
-
是的,但是没有办法跟踪一个泛型函数的值的来源。所以程序员有责任只为来自那些超全局变量的东西调用 sn-p。
-
实际上 - 以“O'Reilly”为例 - 你会得到一个正确转义的字符串“O\'Reilly”......当你想插入一个像“O”这样的字符串时会出现问题\'Reilly"(作为稍后要显示的正确转义字符串的示例) - 因为 DB_Quote 将简单地返回 "O\'Reilly" 而不是 "O\\\'Reilly",然后从数据库中选择它会给你“O'Reilly”
-
@HorusKol:是的,你是对的。很抱歉。
标签: php stripslashes addslashes magic-quotes-gpc