猿问

无需连接数据库即可替代mysql_real_escape_string

我想有一个不连接数据库就表现为mysql_real_escape_string的功能,因为有时我需要在没有数据库连接的情况下进行干式测试。mysql_escape_string已被弃用,因此是不可取的。我的一些发现:


http://www.gamedev.net/community/forums/topic.asp?topic_id=448909


http://w3schools.invisionzone.com/index.php?showtopic=20064


一只名叫tom的猫
浏览 943回答 3
3回答

慕哥6287543

没有DB连接就无法安全地转义字符串。mysql_real_escape_string()并且准备好的语句需要连接到数据库,以便它们可以使用适当的字符集转义字符串-否则,使用多字节字符仍然可能发生SQL注入攻击。如果仅测试,那么您可能还可以使用mysql_escape_string(),它不能100%保证不会受到SQL注入攻击,但是如果没有数据库连接就无法构建任何更安全的东西。

子衿沉夜

好吧,根据mysql_real_escape_string函数参考页:“ mysql_real_escape_string()调用MySQL的库函数mysql_real_escape_string,它转义了以下字符:\ x00,\ n,\ r,\,',“和\ x1a。”考虑到这一点,那么您发布的第二个链接中给出的功能应该完全满足您的需求:function mres($value){    $search = array("\\",  "\x00", "\n",  "\r",  "'",  '"', "\x1a");    $replace = array("\\\\","\\0","\\n", "\\r", "\'", '\"', "\\Z");    return str_replace($search, $replace, $value);}

回首忆惘然

与我的其他答案完全相反,即使使用多字节字符,此后续功能也可能是安全的。// replace any non-ascii character with its hex code.function escape($value) {&nbsp; &nbsp; $return = '';&nbsp; &nbsp; for($i = 0; $i < strlen($value); ++$i) {&nbsp; &nbsp; &nbsp; &nbsp; $char = $value[$i];&nbsp; &nbsp; &nbsp; &nbsp; $ord = ord($char);&nbsp; &nbsp; &nbsp; &nbsp; if($char !== "'" && $char !== "\"" && $char !== '\\' && $ord >= 32 && $ord <= 126)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $return .= $char;&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $return .= '\\x' . dechex($ord);&nbsp; &nbsp; }&nbsp; &nbsp; return $return;}我希望比我自己更有知识的人可以告诉我为什么上面的代码行不通...
随时随地看视频慕课网APP
我要回答