PHP 代码容易受到 SQL 注入:如何使用“mysql_real_escape_string()”?
当我从几年前的教程中学习时,我最终得到了以下容易受到 SQL 注入攻击的代码。
有人可以向我解释如何使用 mysql_real_escape_string() 吗? 这是目前的防弹方法吗?
//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
//Sanitize the POST values
$usr = $_SESSION['usr'];
$live = (isset($_POST['live']))?1:0;
$created = date("F j, Y, g:i a",time()+60*60);
$title= clean($_POST['title']);
$content = clean($_POST['content']);
//Create INSERT query
$qry = "INSERT INTO news( usr, live, created, title, content) VALUES( '$usr', '$live', '$created', '$title', '$content') ";
$result = @mysql_query($qry);
As I was learning from couple of years old tutorials I have end up with following code which IS vulnerable for SQL injections.
Can someone explain to me how to work with mysql_real_escape_string()
?
Is this currently bulletproof method?
//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
//Sanitize the POST values
$usr = $_SESSION['usr'];
$live = (isset($_POST['live']))?1:0;
$created = date("F j, Y, g:i a",time()+60*60);
$title= clean($_POST['title']);
$content = clean($_POST['content']);
//Create INSERT query
$qry = "INSERT INTO news( usr, live, created, title, content) VALUES( '$usr', '$live', '$created', '$title', '$content') ";
$result = @mysql_query($qry);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
附加到查询字符串的所有内容都应该使用 mysql_real_escape_string 进行转义。它将阻止大多数 SQL 注入。但最好使用准备好的语句
示例:
参见PDO::__construct 供参考
Everything you append to query string you should escape with
mysql_real_escape_string
. It will prevent most of SQL injections. But better use prepared statementsExample:
See PDO::__construct for reference
是的。 在这种情况下。
请注意,没有什么比“普遍消毒”更好的了。我们称其为引用,因为这就是它的全部内容。
引用时,总是引用某些特定输出的文本,例如:
like
mysql 查询的表达式对于每种情况,您需要不同的引用,因为每种用法都存在于不同的语法上下文中。这也意味着不应在 PHP 的输入处进行引用,而应在特定的输出处进行引用!这就是像
magic_quotes_gpc
这样的功能被破坏的原因(我建议将其关闭)。那么,在这些特殊情况下,人们会使用什么方法来引用呢? (请随意纠正我,可能有更现代的方法,但这些对我有用)
mysql_real_escape_string($str)
mysql_real_escape_string(addcslashes($str, "%_"))< /code>
htmlspecialchars($str)
json_encode()
- 仅适用于 utf8!我将我的函数用于 iso-8859-2mysql_real_escape_string(addcslashes($str, '^.[]$()|*+?{}'))
- 在这种情况下你不能使用 preg_quote 因为反斜杠将被转义两次!preg_quote()
Yes, it is. In this case.
Note there is nothing like "universal sanitization". Let's call it just quoting, because that's what its all about.
When quoting, you always quote text for some particular output, like:
like
expression for mysql queryFor each case, you need different quoting, because each usage is present within different syntax context. This also implies that the quoting shouldn't be made at the input into PHP, but at the particular output! Which is the reason why features like
magic_quotes_gpc
are broken (I recommend to keep it switched off).So, what methods would one use for quoting in these particular cases? (Feel free to correct me, there might be more modern methods, but these are working for me)
mysql_real_escape_string($str)
mysql_real_escape_string(addcslashes($str, "%_"))
htmlspecialchars($str)
json_encode()
- only for utf8! I use my function for iso-8859-2mysql_real_escape_string(addcslashes($str, '^.[]$()|*+?{}'))
- you cannot use preg_quote in this case because backslash would be escaped two times!preg_quote()
对于黑客攻击来说,没有什么是刀枪不入的。话虽如此,是的.. mysql_real_escape_string() 可以防止 SQL 注入攻击。
Nothing is bulletproof when it comes to hacking; having said that, yes.. mysql_real_escape_string() prevents SQL injection attacks.
我认为你的方法是sql注入保存。
I think your method is sql injection save.
您可以使用 mysql_real_escape_string 来转义危险字符...
you can use mysql_real_escape_string, to escape dangerous characters...
我会做类似的事情
I would do something like
是的,这个很好。 您可以使用准备好的语句,但
不要使用@来隐藏所有错误!
Yes, this one is fine. You can use prepared statements, though
Do not use @ to hide all errors!