这是我的代码:
$email= mysqli_real_escape_string($db_con,$_POST['email']);
$psw= mysqli_real_escape_string($db_con,$_POST['psw']);
$query = "INSERT INTO `users` (`email`,`psw`) VALUES ('".$email."','".$psw."')";
有人可以告诉我它是否安全或者是否容易受到 SQL 注入攻击或其他 SQL 攻击?
有人可以告诉我它是否安全或者是否容易受到 SQL 注入攻击或其他 SQL 攻击?
No.正如 uri2x 所说,请参阅绕过的 SQL 注入mysql_real_escape_string() https://stackoverflow.com/a/12118602/2224584.
防止 SQL 注入的最佳方法是使用准备好的语句。 https://paragonie.com/blog/2015/05/preventing-sql-injection-in-php-applications-easy-and-definitive-guide它们将数据(您的参数)与指令(SQL 查询字符串)分开,并且不会为数据留下任何空间来污染您的查询结构。准备好的语句解决了其中之一应用安全的基本问题 https://paragonie.com/blog/2015/08/gentle-introduction-application-security.
对于无法使用准备好的语句的情况(例如LIMIT
),针对每个特定目的使用非常严格的白名单是唯一的方法保证安全。
// This is a string literal whitelist
switch ($sortby) {
case 'column_b':
case 'col_c':
// If it literally matches here, it's safe to use
break;
default:
$sortby = 'rowid';
}
// Only numeric characters will pass through this part of the code thanks to type casting
$start = (int) $start;
$howmany = (int) $howmany;
if ($start < 0) {
$start = 0;
}
if ($howmany < 1) {
$howmany = 1;
}
// The actual query execution
$stmt = $db->prepare(
"SELECT * FROM table WHERE col = ? ORDER BY {$sortby} ASC LIMIT {$start}, {$howmany}"
);
$stmt->execute(['value']);
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
我认为上面的代码不会受到 SQL 注入的影响,即使是在模糊的边缘情况下也是如此。如果您使用 MySQL,请确保关闭模拟准备。
$db->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)