gpt4 book ai didi

php - 我无法比较数据库中的密码和输入的密码

转载 作者:可可西里 更新时间:2023-11-01 12:42:46 24 4
gpt4 key购买 nike

我正在使用 php crypt 函数来确保密码安全,但是当我尝试将输入的密码与数据库中的密码进行比较时,它不起作用。

首先是我创建密码的代码:

$crypt_password = crypt($_POST['confirm-password']);

这是我尝试与另一个函数中的密码进行比较:

$input_crypt_password = crypt($_POST['input-pw']);

if ($input_crypt_password == $dbpassword){
// do change password function
}

这是行不通的。

当我打印两个密码时,它们是不同的。

为什么我输入相同的密码并在两者上都使用了 crypt 函数,但密码却不同?

谁能指出我正确的方向?

最佳答案

来自 the docs

Example #1 crypt() examples

<?php
$hashed_password = crypt('mypassword'); // let the salt be automatically generated

/* You should pass the entire results of crypt() as the salt for comparing a
password, to avoid problems when different hashing algorithms are used. (As
it says above, standard DES-based password hashing uses a 2-character salt,
but MD5-based hashing uses 12.) */
if (hash_equals($hashed_password, crypt($user_input, $hashed_password))) {
echo "Password verified!";
}
?>

问题中的代码每次被调用时都会有效地生成一个新的哈希 - 现有的密码哈希需要作为盐传递以获得一致的结果。

正如文档中提到的:

Use of password_hash() is encouraged.

我会更进一步说你绝对应该使用 password_hash 而不是调用 crypt 来使用密码(假设 php >= 5.5);在任何情况下,无论您使用何种工具/方法 - 请阅读文档以了解如何使用它们。

关于php - 我无法比较数据库中的密码和输入的密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41141215/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com