鉴于此代码:
mysqli_set_charset('utf8');
$id = mysqli_real_escape_string($_GET['id']);
$result = mysqli_query($con,"SELECT * FROM post WHERE id_post = '$id'");
if (mysqli_num_rows($result) == 0) {
header('Location: 404.php');
die();
} else {
// Continue...
}
而且:
$id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT);
// New PDO...
$Ps = $Pdo->prepare('SELECT * FROM post WHERE id_post = :id');
$Ps->execute(array(':id', $id));
if ($Ps->rowCount() == 0) {
header('Location: 404.php');
die();
} else {
// Continue...
}
是否有防止SQL注入或等效的最佳选择?
答案 0 :(得分:1)
第二种选择是要走的路。使用mysql_real_escape_string()
使用一些异乎寻常的多字节字符为sql注入打开一些空间。参考:SQL injection that gets around mysql_real_escape_string()
或者,您可以将准备好的陈述与mysqli