Joh*_*aig 6 php mysql sql-injection code-injection
我写了这个简短的函数来防止my_sql注入,因为它的重要性我只想与其他人仔细检查这将按照我的意图运行.
foreach($_REQUEST as $key => $value) {
$_REQUEST[$key] = stripslashes($value);
$_REQUEST[$key] = mysql_real_escape_string($_REQUEST[$key]);
}
Run Code Online (Sandbox Code Playgroud)
TMS*_*TMS 11
好吧,你使用stripslashes()因为magic_quotes_gpc设置了吗?因此,此代码仅在magic_quotes_gpc设置时才有效!我建议你关闭它,不要使用strislashes()调用.
但请注意,没有什么比"普遍消毒"更像.我们称之为引用,因为这就是它的全部内容.
在引用时,您总是引用某些特定输出的文本,例如:
like mysql查询的表达式对于每种情况,您需要不同的引用,因为每种用法都存在于不同的语法上下文中.这也意味着引用不应该在PHP的输入中进行,而是在特定的输出中进行!这就是为什么功能magic_quotes_gpc被打破的原因(总是保证它被关闭!!!).
那么,在这些特定情况下,用什么方法来引用?(随意纠正我,可能有更现代的方法,但这些对我有用)
mysql_real_escape_string($str)mysql_real_escape_string(addcslashes($str, "%_"))htmlspecialchars($str)json_encode() - 仅限utf8!我将我的功能用于iso-8859-2mysql_real_escape_string(addcslashes($str, '^.[]$()|*+?{}')) - 在这种情况下你不能使用preg_quote,因为反斜杠会被转义两次!preg_quote()如果你使用PDO(正确),你不必担心MySQL注入.
样品:
/* Execute a prepared statement by passing an array of insert values */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < :calories AND colour = :colour');
$sth->execute(array(':calories' => $calories, ':colour' => $colour));
Run Code Online (Sandbox Code Playgroud)