【问题标题】:AJAX Password ChangeAJAX 密码更改
【发布时间】:2012-10-20 19:23:18
【问题描述】:

所以我正在尝试在我的网站上实现密码更改功能,并且我想提交密码表单而不刷新页面。所以我正在尝试使用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 输入的内容。如果它们匹配,则它会更改密码。

【问题讨论】:

  • 也制作 $('#success_div').html(response); //正确更新DIV

标签: php jquery html sql pdo


【解决方案1】:

你的 JS 有点不对劲。看我的cmets:

$('#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-04-16
  • 2012-10-11
  • 2019-05-09
  • 2018-01-07
  • 2017-10-05
  • 2011-02-22
  • 2013-09-17
  • 2011-06-29
  • 1970-01-01
相关资源
最近更新 更多