我正在構建一個儀表板系統,Apache 在樹莓派上運行,并為所有新用戶預先生成密碼及其哈希值。
我以前這樣做的行是password_hash('Password1@', PASSWORD_DEFAULT).
用戶首次登錄時會顯示一個密碼重置視窗。我能夠成功使用password_hash(),并且password_verify()在用戶單擊此密碼重置頁面上的提交后。
第一次登錄作業正常,但在注銷后的任何登錄嘗試都會導致password_verify()失敗。
我檢查過/嘗試過的
- 將資料庫中的密碼屬性設定為
varchar(255). - 檢索到單個用戶行,我可以從中回傳資料。
PASSWORD_DEFAULT兩者PASSWORD_ARGON2ID都這樣做。
我知道的事情
- 資料庫是
utf8mb4_unicode_ci. - 用戶設定的新密碼成功推送到資料庫。
- 我添加了一條
if陳述句來檢查是否可以驗證新的哈希值并且可以。
- 我添加了一條
- 哈希字串與 中回傳的內容匹配
SELECT,因為它應該。 - 我在其他專案中使用了以下函式,使用 PHP 7。這個專案是在 PHP 8 上的。(這可能是問題嗎?)
密碼重置功能
public function firstLoginUpdatePassword($username, $password, $confirm, $token)
{
if ($password != $confirm)
{
header("Location: first-login?mismatch&token=" . $token);
exit;
}
else
{
$newPassword = password_hash($password, PASSWORD_DEFAULT);
$token = bin2hex(openssl_random_pseudo_bytes(16));
try
{
$stmt = $this->con->prepare("UPDATE Account SET Password=:password, isFirstLogin=FALSE, Token=:token WHERE Username=:username");
$stmt->bindparam(":username", $username);
$stmt->bindparam(":password", $newPassword);
$stmt->bindparam(":token", $token);
if ($stmt->execute())
{
header("Location: home");
exit;
}
else
{
header("Location: first-login?error&token=" . $token);
exit;
}
}
catch (PDOException $ex)
{
echo $ex->getMessage();
}
}
}
登錄功能
public function Login($user, $pwd)
{
try
{
$stmt = $this->con->prepare("SELECT Username, Password FROM Account WHERE Username=:username or Email=:username;");
$stmt->bindparam(":username", $user);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ($stmt->rowCount() == 1)
{
if (password_verify($pwd, $row['Password']))
{
try
{
$stmt = $this->con->prepare("UPDATE Account SET LastLogin=CURRENT_TIMESTAMP WHERE Username=:username;");
$stmt->bindparam(":username", $row['Username']);
if ($stmt->execute())
{
$_SESSION['userSession'] = $row['Username'];
return true;
}
else
{
header("Location: login?error-other");
exit;
}
}
catch (PDOException $ex)
{
echo $ex->getMessage();
}
}
else
{
header("Location: login?error-credential");
exit;
}
}
else
{
header("Location: login?error-login");
exit;
}
}
catch(PDOException $ex)
{
echo $ex->getMessage();
}
}
uj5u.com熱心網友回復:
解決方案在檔案編碼的某個地方。服務器上我的 PHP 檔案有多種編碼。我將所有檔案遞回地重新編碼為 UTF-8,現在問題已經消失了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/490501.html
