以下代码是否容易受到Sql注入以及如何验证?
$query=("select * from table2 where username = '$username'and password='$password'");
$result= mysqli_query($connect, $query);
$row= mysqli_fetch_assoc($result);
if ($username==$row['username']&&$password==$row['password'])
{
header('location:header.php');//to go header
}
else
{
header('location:insert_p.php');}
答案 0 :(得分:1)
是的,您的代码似乎容易受到SQL注入攻击。看看这一行:
$query=("select * from table2 where username = '$username' and password='$password'");
在这里,您将变量$username
和$password
直接传递给数据库。如果$username
包含类似admin'; --
的字符串,则不会检查密码。
请确保您直接放入SQL语句的每个变量都是安全的。
mysqli_real_escape_string()
(docs)来转义'
等特殊字符。