Phpass - 如何根据数据库中的用户名和密码哈希检查登录用户名和密码

标签 php mysql database hash phpass

我已经成功地使用 Phpass 对注册用户密码进行哈希处理并将它们存储在数据库中,现在我卡在了登录名如何检查提交的用户名和密码,检查用户名是否存在于数据库中然后检查哈希密码与给定一个。

非常感谢任何帮助!!!谢谢!

这是我的代码:

<?php

// Inialize session
session_start();

// Include database connection settings
include('config.inc');

require("PasswordHash.php");
$hasher = new PasswordHash(8, false);

$username = $_POST['username'];
$password = $_POST['password'];

// Passwords should never be longer than 72 characters to prevent DoS attacks
if (strlen($password) > 72) { die("Password must be 72 characters or less"); }

$query = "SELECT * FROM user WHERE username = '$username'";

$query = mysql_query($query);
$numrows = mysql_num_rows($query);

if ($numrows = 1) {


$res = mysql_query("SELECT password FROM user WHERE username = '$username'"); 
$row = mysql_fetch_array($res); 
$hash = $row['password']; 
$password = $_POST['password'];

if ($hasher->CheckPassword($password, $hash)) { //$hash is the hash retrieved from the      DB 
        $what = 'Authentication succeeded';
    } else {
        $what = 'Authentication failed';
    }

} else {

 echo "No Such User";
include 'login.php';
exit();
}

echo "$what\n";
echo "<br />";
echo "$hash";

?>

这是我为了他人的利益而工作的代码:

<?php

// Inialize session
session_start();

// Include database connection settings
include('config.inc');

require("PasswordHash.php");
$hasher = new PasswordHash(8, false);

$username = $_POST['username'];
$password = $_POST['password'];

// Passwords should never be longer than 72 characters to prevent DoS attacks
if (strlen($password) > 72) { die("Password must be 72 characters or less"); }

$query = "SELECT * FROM user WHERE username = '$username'";

$query = mysql_query($query);
$numrows = mysql_num_rows($query);

if ($numrows = 1) {


$res = mysql_query("SELECT * FROM user WHERE username = '$username'"); 
$row = mysql_fetch_array($res); 
$hash = $row['password']; 
$password = $_POST['password'];

if ($hasher->CheckPassword($password, $hash)) { //$hash is the hash retrieved from the      DB 
        $what = 'Authentication succeeded';
    } else {
        $what = 'Authentication failed';
    }

} else {

 echo "No Such User";
include 'login.php';
exit();
}

echo "$what\n";
echo "<br />";
echo "$hash";

?>

最佳答案

这是如何phpass工作原理:当您保存用户密码(当他们创建密码时)时,您会在保存之前对其进行哈希处理,如下所示:

$hash_iterations = 30;
$portable_hashes = FALSE;
$hasher = new PasswordHash($hash_iterations, $portable_hashes);
$hash_value = $hasher->HashPassword($actual_password);

然后在数据库中保存$hash_value作为用户的密码。当您去验证用户时,按用户名查找用户。如果找到,将数据库中的实际密码(存储的哈希值)与用户输入的哈希值进行比较:

// $stored_hash is the value you saved in the database for this user's password
// $user_input is the POST data from the user with the actual password
$valid_password = $hasher->CheckPassword($user_input, $stored_hash);

确保每次都以相同的方式初始化 PasswordHash 类,使用相同的 $hash_iterations$portable_hashes 值,或者比较将无法正常工作。

关于Phpass - 如何根据数据库中的用户名和密码哈希检查登录用户名和密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12273314/

相关文章:

MySQL 查询 SELECT 多行

mysql - 外键和基本关系表

php - 如何从 cake php 中的 url 获取 id

php - PHP 的 gmtime() 在哪里?

javascript - 计算数组中的单词而不是字符

Java Applet - 可以将数据插入 MySQL 数据库吗?

java - SQL 多条件

php - 我的 PDO 函数出错

php - 使用数据库中的值进行计算

mysql - 获取列名而不是列值