我試圖開發一個學校項目的登錄系統,但我遇到了錯誤,我不知道如何解決它。從數據庫獲取沒有數據
在我的數據庫中,我有密碼spy123
存儲在sha256哈希,但是當我試圖抓住它在PHP中,我沒有得到任何回報。
當前代碼:
// checks if the user/password combination works.
// $username | String - the username of the user to login
// $password | 32byteHash - the hashed password of the user
// return | Boolean - Returns true if the password works for that user.
function forumLib_checkLogin($username, $password){
$goodLogin = false; //Assume the password is wrong.
$obtainUserPassword_query = "SELECT password FROM forum_users WHERE username = '$username'";
$dbc = forumLib_connectToDB();
$results = mysqli_query($dbc, $obtainUserPassword_query);
forumLib_closeConnectionToDB($dbc);
echo "query: $obtainUserPassword_query <br />";
echo "results: ".mysqli_fetch_array($results)."<br />";
$row = mysqli_fetch_array($results);
echo "row[0]: ".$row[0]."<br />";
echo "password: $password <br />";
echo "DBpassword: $passwordOnDB <br />";
//if user exists
if($results != false){
//get password from DB
$row = mysqli_fetch_array($results);
$passwordOnDB = $row['password'];
//check password
if($password == $passwordOnDB){
//successfull login
$goodLogin = false;
}
}
return $goodLogin;
}
輸出:
change password request detected
query: SELECT password FROM forum_users WHERE username = 'SpyMaster356'
results: Array
row[0]:
password: 7d9519224964c72a90b342c565fab1e4582d0e974043798f940b471304db0e24
DBpassword:
Current password does not match records.
SQL查詢是沒有錯的,因爲我得到7d9519224964c72a90b342c565fab1e4582d0e974043798f940b471304db0e24
回來時,我手動輸入。
這真讓我困惑。
而不是echo使用print_r($ row)來查看真正從數據庫中提取的內容。您還應該測試mysqli_query()調用的返回值以查看它是否失敗 – Cfreak 2013-02-28 17:58:06
每執行一次'mysqli_fetch_array',結果集中的光標都會遞增。 – hjpotter92 2013-02-28 17:58:19
另外,輸出'7d9519224964c72a90b342c565fab1e4582d0e974043798f940b471304db0e24'不是由查詢生成的;它是由提供給函數的'$ password'生成的。 – hjpotter92 2013-02-28 18:02:51