你好,我只想问一下关于SQL注入的问题,我目前正在处理一个登录页面,但是我遇到了一些关于SQL注入的问题,我目前正在测试一个批处理的SQL代码,如下所示,我还没有设置一个SQL参数,但是它似乎没有在执行SQL注入。我的验证是基于行计数的,如果它等于0,它将销毁会话并再次重定向到索引。代码似乎运行良好,但我担心它为什么在不添加任何SQL参数的情况下正常工作,以防止SQL注入。我希望有人能解释一下,谢谢
secured_page.php
<?php
// Start the session
session_start();
// Set session variables
$_SESSION["email"] = $_POST['email'];
$_SESSION["password"] = md5($_POST['password']);
if (isset($_SESSION['email'])){
header('Location: profile.php');
}
else {
header('Location: index.php');
}
?>profile.php
<?php
// Start the session
session_start();
include('header.php');
include('db_connect.php');
$email = $_SESSION["email"];
$password = $_SESSION["password"];
$sql = "SELECT * FROM user where email = '$email' and password = '$password' LIMIT 1";
$result = $conn->query($sql);
echo $result->num_rows;
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["name"]. " " . $row["email"]. "<br>";
}
} else {
header('Location: unset_session.php');
}
if (!isset($_SESSION['email'])){
header('Location: index.php');
}
?>
<br>
<a href="unset_session.php">Logout</a>
<?php
$conn->close();
include('footer.php');
?>发布于 2015-12-11 06:08:33
这样做可以防止SQL注入。
$sql = "SELECT * FROM user where email = ? and password = ? LIMIT 1";
$result = $mysqli->prepare($sql);
$result->bind_param("ss", $email, $password);
/* execute query */
$result->execute();
//Now you can use $result variable like you used before
echo $result->num_rows;在这里了解更多关于准备语句的信息:http://php.net/manual/en/mysqli.prepare.php
https://stackoverflow.com/questions/34216829
复制相似问题