我目前正在升级朋友网站的代码,并且遇到了登录脚本的问题。我正在尝试使用mysql_real_escape_string()
来阻止SQL注入。 mysql_escape_string()
工作正常但mysql_real_escape_string()
没有。这就是我所拥有的:
if (isset($_POST['submit']))
{
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$db_link = db_connect("project");
$query = "SELECT * FROM user WHERE username = '$username' AND password = password('$password')";
$result = mysql_query($query) or die (mysql_error());
$numrows = mysql_num_rows($result);
if($numrows == 1)
{
RedirectToURL("home.php");
}
else
{
login_page("Invalid login! Try again");
}
}
else
{
login_page("");
}
答案 0 :(得分:6)
要使用mysql_real_escape_string,您必须登录数据库。 如果您尚未登录,请使用mysql_escape_string。
答案 1 :(得分:2)
正如@MichaelBerkowski在你的评论中所说,将mysql_real_escape_string()
个电话移到你的连接命令之后:
if (isset($_POST['submit']))
{
$db_link = db_connect("project");
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$query = "SELECT * FROM user WHERE username = '$username' AND password = password('$password')";
$result = mysql_query($query) or die (mysql_error());
$numrows = mysql_num_rows($result);
if($numrows == 1)
{
RedirectToURL("home.php");
}
else
{
login_page("Invalid login! Try again");
}
}
else
{
login_page("");
}
如上所述,原因是that mysql_real_escape_string()
取决于你的mysql连接工作