【问题标题】:Delete account with password verification使用密码验证删除帐户
【发布时间】:2021-10-19 20:13:57
【问题描述】:

我有这个解决方案供用户删除他的帐户(效果很好)

        if(!empty($_POST['delete'])){
    
    if(empty($_POST['currentpassword'])) {
            $error = error('Please enter current password.');
    }

    $SQLCheckCurrent = $odb -> prepare("SELECT COUNT(*) FROM `users` WHERE `ID` = :ID AND `password` = :currentpassword");
    $SQLCheckCurrent -> execute(array(':ID' => $_SESSION['ID'], ':currentpassword' => SHA1(md5($_POST['currentpassword']))));
    $countCurrent = $SQLCheckCurrent -> fetchColumn(0);

    if ($countCurrent == 0){
        $error = error('Current password is incorrect.');
    }
    
    $notify = error($error);

    if(empty($error)){
        $SQLUpdate = $odb -> prepare("DELETE FROM `users` WHERE `username` = :username AND `ID` = :id");
        $SQLUpdate -> execute(array(':username' => $_SESSION['username'], ':id' => $_SESSION['ID']));
        session_destroy();
        header("location: login");
        die();
    }

}

我最近将所有内容从 MD5 更改为较新的 password_hash,但遗憾的是我无法更新这部分,我已经尝试过,但到目前为止没有运气

        if(!empty($_POST['delete'])){
    
    if(empty($_POST['currentpassword'])) {
            $error = error('Please enter current password.');
    }

    $SQLCheckCurrent = $odb -> prepare("SELECT COUNT(*) FROM `users` WHERE `ID` = :ID AND `password` = :currentpassword");
    $SQLCheckCurrent -> execute(array(':ID' => $_SESSION['ID'], ':currentpassword' => password_verify($_POST['currentpassword'],$userInfo['password'])));
    $countCurrent = $SQLCheckCurrent -> fetchColumn(0);

    if ($countCurrent == 0){
        $error = error('Current password is incorrect.');
    }
    
    $notify = error($error);

    if(empty($error)){
        $SQLUpdate = $odb -> prepare("DELETE FROM `users` WHERE `username` = :username AND `ID` = :id");
        $SQLUpdate -> execute(array(':username' => $_SESSION['username'], ':id' => $_SESSION['ID']));
        session_destroy();
        header("location: login");
        die();
    }

}

【问题讨论】:

    标签: php password-encryption password-hash


    【解决方案1】:

    我建议先阅读有关如何使用 password_verify 的信息,因为 password_verify 只返回给您 TRUE/FALSE 的布尔值。因此,您不能使用该函数来检查数据库中的数据。

    在此处了解密码验证:https://www.php.net/manual/en/function.password-verify.php

    然后,对于解决方案,您可以像这里一样更改代码的逻辑:

    1.仅通过 ID 获取保存的密码(自行查询),例如我会叫它$saved_password

    $saved_password = '...'; // Get the password from DB by user id
    

    2。使用保存的密码验证给定的密码

    if (password_verify($_POST['currentpassword'], $saved_password)) {
      // Password verified
      // DELETE THE USER
    } else {
      // Error. The password is wrong!
    }
    

    就是这样,希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2020-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-27
      • 2014-10-01
      相关资源
      最近更新 更多