我已將password_hash
PHP函數添加到我的註冊頁面,用戶可在其中創建帳戶和存儲密碼。我在註冊表格中應用了相同的password_hash
代碼行。數據保存在數據庫中,但是當我嘗試登錄時,提交的密碼與存儲的密碼不匹配。我也嘗試了我在PHP手冊中讀到的password_verify
函數,但沒有任何工作。在嘗試和嘗試之後,我真的不知道還有什麼要做,所以如果有人有建議,我會很感激。無法登錄。提交的密碼與存儲在數據庫中的密碼不匹配。 - PHP,Password_hash
P.S.我目前使用PHP版本5.6.25。
下面的代碼:
<?php
// AJAX CALLS THIS LOGIN CODE TO EXECUTE
if(isset($_POST["e"])){
// CONNECT TO THE DATABASE
include_once("PHP_Includes/db.php");
// GATHER THE POSTED DATA INTO LOCAL VARIABLES AND SANITIZE
$e = mysqli_real_escape_string($db, $_POST['e']);
$p = password_hash($_POST['p'], PASSWORD_DEFAULT,['cost' => 15]);
// FORM DATA ERROR HANDLING
if($e == "" || $p == ""){
echo "login_failed";
exit();
} else {
// END FORM DATA ERROR HANDLING
$sql = "SELECT id, username, password FROM users WHERE email='$e' AND activated='1' LIMIT 1";
$query = mysqli_query($db, $sql);
$row = mysqli_fetch_row($query);
$db_id = $row[0];
$db_username = $row[1];
$db_pass_str = $row[2];
if($p != $db_pass_str){
echo "login_failed";
exit();
} else {
// CREATE THEIR SESSIONS
$_SESSION['userid'] = $db_id;
$_SESSION['username'] = $db_username;
$_SESSION['password'] = $db_pass_str;
}
}
exit();
}
?>
login()函數:
function login(){
var e = _("email").value;
var p = _("password").value;
if(e == "" || p == ""){
_("status").innerHTML = "Fill out all of the form data";
} else {
_("loginbtn").style.display = "none";
_("status").innerHTML = 'please wait ...';
var ajax = ajaxObj("POST", "index.php");
ajax.onreadystatechange = function() {
if(ajaxReturn(ajax) == true) {
if(ajax.responseText == "login_failed"){
_("status").innerHTML = "Login unsuccessful, please try again.";
_("loginbtn").style.display = "block";
} else {
window.location = "user.php?u="+ajax.responseText;
}
}
}
ajax.send("e="+e+"&p="+p);
}
}
哪裏是使用'password_verify()'你的企圖?這就是你需要使用的。 ''password_hash()'每次調用都會給你不同的結果。 –