-1

ユーザーのハッシュ化されたパスワードを、データベースに保存したパスワードと照合しようとしています。this guyとほぼ同じ問題ですが、PDO で実行しようとしていますが、データベースからハッシュ化されたパスワードを取得してチェックする方法がわかりません。これまでのログインページのコードは次のとおりです。

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL); ini_set('display_errors', 1);

require_once "/home/carlton/public_html/PHPproject/includes/PasswordHash.php";

if ($_POST){
$form = $_POST;
$username = $form['username'];
$password = $form['password'];


try{
    $db = new PDO('mysql:host=localhost;dbname=phpproject', 'root', 'pdt1848!');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
    catch(PODException $e){
        echo "Can't connect to the database";
    }
$sql = "SELECT * FROM users WHERE username=:username";
$query = $db->prepare($sql);
$query->execute(array(':username'=>$username, ':password'=>$stored_hash));
$results = $query->fetchAll(PDO::FETCH_ASSOC);


$check = $hash_obj->CheckPassword($password, $stored_hash);
if($check){
    print_r("Registered user");
}
else{
    print_r("Not a registered user");
}


//login here
} 
else{
?>
<form name="login" action="login.php" method="POST">
<label for "username">Username: </label>
<input type="text" name="username"/><br />
<label for "password">Password: </label>
<input type="password" name="password"/><br />
<button type="submit">Submit</button>
<button type="reset">Reset Form</button>
</form>
<?php
}
?>
4

4 に答える 4

1

それは簡単です。

最初に保存されたパスワードを選択してから、それを確認する必要があります。

于 2014-04-29T06:10:13.280 に答える
0

You're really just comparing the hashed password you have in your DB matches the inputted password, once it too is similarly hashed.

  • Firstly, don't use the root account to access your DB
  • Try to avoid SELECT * - just select the columns you actually need.
  • The number of tokens (:username) must equal the number of bound params (':username'=>$username) in your query. Otherwise PDO will throw an error.
  • You have not posted the class that is handling the password verification itself, but it's basically just hashing the input and comparing the two, returning a bool.
  • Incidentally, print_r is to echo out the values of variables, arrays in particular. If you're just echoing a string as a response, then just print or echo is fine.
于 2014-05-02T01:31:10.743 に答える