0

そのため、自分のサイトにパスワード変更機能を実装しようとしており、ページを更新せずにパスワード フォームを送信したいと考えています。だから私はajaxを使用しようとしています。これが私のhtmlです:

<form id="change_Pass" action="" method="post">
    Current Password<input type="password" id="change_password" name="change_password"><br>
    New Password<input type="password" id="new_password" name="new_password"><br>
    Verify Password<input type="password" id="verify_password" name="verify_password"><br>
    <input type="submit" value="Submit">
</form>

そしてjquery:

$('#change_Pass').submit(function(e){
    $.ajax({
        data: $(this).serialize(), // get the form data
        type: $(this).attr('POST'), // GET or POST
        url: $(this).attr('Private/change_password.php'), // the file to call
        success: function(response) { // on success..
            $('#success_div).html(response); // update the DIV
        },
        error: function(e, x, r) { // on error..
            $('#error_div).html(e); // update the DIV
        }
    });
    e.preventDefault();
});

そしてphp:

<?php
$usr = $_SESSION["username"];
$old_pwd = $_POST["change_password"];
$new_pwd = $_POST["new_password"];

$link = new PDO('mysql:host=*;dbname=*;charset=UTF-8','*','*');
$query = "SELECT *
        FROM Conference
        WHERE Username = :un";

$stmt = $link->prepare($query);

$stmt->bindParam(':un', $usr);
$stmt->execute();
$row = $stmt->fetchAll();

$hash = $row[0]["Password"];
$is_correct = Bcrypt::check($old_pwd, $hash);
if($is_correct) {
    $query = "UPDATE Conference
            SET `Password`=:new_pwd 
            WHERE Username = :usr";

    $stmt = $link->prepare($query);
    $stmt->bindParam(':new_pwd', $new_pwd);
    $stmt->bindParam(':usr', $usr);
    $stmt->execute();
}

しかし、私はいくつかのことにこだわっています。
1) データをシリアル化するのではなく、change_password.php に投稿して使用できるようにするにはどうすればよい$_POSTですか?
2) change_password は正しいように見えますか? current password基本的に、データベース内の既存のパスワードを使用して、その人が何を入力したかをチェックしています。それらが一致すると、パスワードが変更されます。

4

1 に答える 1

1

あなたのJSは少しずれていました。私のコメントを参照してください:

$('#change_Pass').submit(function(e) {
    var $this = $(this);                    // It's a good to cache stuff

    $.ajax({
        data: $this.serialize(),
        type: $this.attr('method'),         // You want `method` here
        url: 'Private/change_password.php', // Dunno why you used `attr`
        success: function(response) {
            $('#success_div').html(response);
        },
        error: function(e, x, r) {
            $('#error_div').html(e);
        }
    });

    e.preventDefault();
});

また、あなたのパスワード変更ロジックは私には正しくありません。Bcrypt を使用しているため、ユーザーのパスワードをプレーンテキストで保存する必要はありません(必要になることはありません)。

パスワードの代わりにパスワードの Bcrypt ハッシュを保存します。これこそが、パスワード ハッシュの要点です。

于 2012-10-20T19:24:48.927 に答える