0
我對新的php感到困惑...php在更改密碼之前驗證密碼
我寫下面的代碼,但是有些東西沒有工作。
方案是驗證數據庫中的密碼,如果密碼正確,那麼響應應該是確定的。如果沒有,則停止搜索。
$currentPassword = preg_replace('/\s+/', '', $_POST['currentPassword']);
$newPassword = preg_replace('/\s+/', '', $_POST['newPassword']);
$ConfirmPassword = preg_replace('/\s+/', '', $_POST['ConfirmPassword']);
$oldpass = IrBuscarPassword($_SESSION['user']['username']);
$hash = password_hash($oldpass, PASSWORD_BCRYPT, ['cost' => 13]);
if (password_verify($currentPassword, $hash)) {
// password valid
$change = true;
} else {
$change = false;
}
所以......這是我的代碼現在...我不知道這是否是好辦法... 請幫我這...
if(isset($_POST['currentPassword']))
{
$currentPassword = $_POST['currentPassword'];
$newPassword = $_POST['newPassword'];
$ConfirmPassword = $_POST['ConfirmPassword'];
//$saltcode = IrBuscarSalt($_SESSION['user']['username']);
$oldpass = IrBuscarPassword($_SESSION['user']['username']);
$hash = password_hash($oldpass, PASSWORD_BCRYPT, ['cost' => 13]);
if (password_verify($oldpass, $hash)) {
// password valid
//echo $oldpass;
$change = true;
} else { echo "bad"; }
}
從數據庫中提取用戶。使用password_verify檢查提供的「oldpass」(currentPassword),如果有效,則將新密碼的散列保存到數據庫。如果沒有 - 顯示錯誤。 – JimL
雖然我會跳過代碼的'preg_replace'部分,但您可能需要使用舊密碼,因爲存儲的哈希是來自沒有空格的密碼。但是沒有理由限制新哈希的熵 – JimL
更改用戶的輸入密碼是一個壞主意,因爲它們輸入的內容可能與存儲爲密碼的內容不匹配。如果用戶故意在密碼的末尾放置空格以摒棄簡單的暴力攻擊,該怎麼辦? – GordonM