我想驗證用戶輸入的密碼與數據庫中的密碼。我已經知道它會檢查用戶名是否正確(如果用戶名不存在,它會顯示錯誤),但是當它嘗試使用mysql密碼驗證密碼時,它永遠不會工作。工作'例子'在http://scapersclearing.com/fansite/login.php;這是PHP(注意base.php包含數據庫信息和header.php,navigation.php和footer.php以及所有的前端)。我正計劃添加html實體,並在此工作後阻止SQL注入。用戶名:測試 - 密碼:password89(MD5 c1c2434f064da663997b1a2a233bf9f6)數據庫「驗證」PHP
<?php
include("base.php"); //Include MySQL connection
$username = $_POST['username']; //Connect form username with strings
$password = $_POST['password']; //Connect form password with strings
$salt = "xia8u28jd0ajgfa"; //Define the salt string
$salt2 = "oqipoaks42duaiu"; //Define the second salt string
$password = md5($salt.$password.$salt2); //Encrypt the password
$result = mysql_query("SELECT * FROM members WHERE username = '".$username."'"); //Open the members table
while($row = mysql_fetch_array($result)) { //Convert the members table into an array
if ($username != $row['username']) { //If user entered username doesn't equal the database username
include("header.php"); //Print the message
include("navigation.php");
echo "Invalid username or password!";
include("footer.php");
}
else {
if ($row['password'] == $password) { //Validate username and password
setcookie('c_username', $username, time()+6000); //Set the username cookie
setcookie('c_password', $password, time()+6000); //Set the cookie
header("Location:index.php"); //Redirect to home page
} else {
include("header.php"); //Print the message
include("navigation.php");
echo "<div class=\"content\"><p>Invalid username or password!<p></div>";
include("footer.php");
} } }
?>
有它呼應了哈希數據庫以及腳本創建的散列,以查看它們是否匹配。 – 2009-12-11 02:20:42
使用'SELECT * FROM MEMBERS WHERE username =?和password =?' - 如果返回一行,則密碼正確,否則不正確。你的數據庫沒有驗證密碼 - 在你的例子中邏輯完全是PHP。 – 2009-12-11 02:23:21
另外,在將用戶名直接傳遞到查詢中時,您有一個巨大的潛在SQL注入漏洞。改用PDO。 – benjy 2009-12-11 02:27:39