- 我已将用户ID和密码(PHPAPS加密)插入数据库
- 当用户登录时,系统会比较匹配的用户ID和密码
- 但是,由于我将输入密码与存储密码进行比较通过哈希输入密码,它总是返回"错误的密码"
我的代码看起来如下。我在做什么错?
if(isset($_POST["btn_submitlogin"])){
$userpass1 = "admin1234";
$this->load->library('phpass');
$this->load->database();
$hashed1 = $this->phpass->hash($userpass1);
$userpass2 = "admin1234"; // For example, I load the DB password here
$this->load->database();
$hashed2 = $this->phpass->hash($userpass2);
if ($this->phpass->check($hashed1, $hashed2))
echo 'logged in';
else
echo 'wrong password';
}
如果保存在数据库中的密码已经进行了哈希(正如应该的那样,您只需要哈希从用户输入中获取的密码,然后将其与数据库中已有的哈希值进行比较。
phpass库有您可以查看的手册,该手册提供了有关如何正确使用其方法的教程(以及如何防止常见的漏洞(例如SQL注入)。
手册中,我看到有一种称为 CheckPassword($password, $hash)
的方法,它返回布尔值。
这个想法是,您将原始密码从用户输入作为第一个参数传递,然后从数据库中传递Hashed值作为第二个参数。如果密码匹配密码(phpass在内部进行哈希和检查),则返回true,或者如果不匹配。
。,例如
$pass = $_POST['password']; // Your user input.
// .. Check the existence of your user in the DB, and fetch the hashed password (as $hash) if they exist.
if($phpass->CheckPassword($pass, $hash)) {
// Authenticated!
} else {
/// Incorrect password.
}