PHP - 安全更改密码表单/方法

标签 php mysql

我已经根据 Wikihow 文章制作了自己的登录系统,并且想要更改密码字段,但我不知道应该如何做。这是我现在拥有的:

JavaScript

function formchange(form, current, newpass) {
    // Create a new element input, this will be our hashed password field. 
    var p = document.createElement("input");
    var p2 = document.createElement("input");

    // Add the new element to our form. 
    form.appendChild(p);
    p.name = "p";
    p.type = "hidden";
    p.value = hex_sha512(current.value);

    form.appendChild(p2);
    p.name = "p2";
    p.type = "hidden";
    p.value = hex_sha512(newpass.value);

    // Make sure the plaintext password doesn't get sent. 
    current.value = "";
    newpass.value = "";

    // Finally submit the form. 
    form.submit();
}

PHP

if (isset($_POST['cpass'], $_POST['npass'])) {
    $cpass = $_POST['cpass'];
    $npass = $_POST['npass']; // The hashed password.

    echo "Here";

    $un = getUser();

    if ($cpass == $npass) {
        echo "If";
        $random_salt = hash('sha512', uniqid(mt_rand(1, mt_getrandmax()), true));
        $password = hash('sha512', $npass . $random_salt);

        // Insert the new user into the database 
                if ($insert_stmt = $mysqli->prepare("MYSQL UPDATE STATEMENT")) {
                    $insert_stmt->bind_param('p', $password);
                    $insert_stmt->bind_param('s', $random_salt);
                    echo "Binded";
                    // Execute the prepared query.
                    if (! $insert_stmt->execute()) {
                        header('Location: ../home.php?msg=1');
                    }
                }

        header('Location: ../home.php?msg=2');
    } else {
        // Failed
        header('Location: ../?error=1');
    }
} else {
    // The correct POST variables were not sent to this page. 
    echo 'Invalid Request';
}

它似乎不起作用,它一直转到 home.php?msg=2,但数据库没有更新。我对这一切都是新手,学习它是我的项目之一,所以对糟糕的代码表示歉意。我的MySQL语句是这样的:

UPDATE user_data SET user_data.password = 'p', user_data.salt = 's' WHERE username = '$un';

最佳答案

您的脚本设置为在多种条件下转发到 home.php?msg=2,包括查询成功时。

if (! $insert_stmt->execute()) {
    header('Location: ../home.php?msg=1');
}

如果查询执行失败,这会将其发送到 home.php?msg=1,请注意 !。由于查询没有失败,脚本将继续,下一个函数是...

header('Location: ../home.php?msg=2');

“它似乎不起作用,它一直转到 home.php?msg=2。”

那么您希望脚本做什么?你如何设计它,它就应该这样做。

<小时/>

新编辑更新

您应该避免使用这样的 header 位置,否则您将永远无法找到错误。在开发过程中,您应该完全避免使用 header 位置,尤其是在查询失败时。

我将更多地包含 header 位置并捕获错误以查看语句失败的原因。我从来不使用 mysqli,因为在我看来,pdo 更好,但我认为这应该可行。

           if ($insert_stmt = $mysqli->prepare("MYSQL UPDATE STATEMENT")) {
                $insert_stmt->bind_param('p', $password);
                $insert_stmt->bind_param('s', $random_salt);
                echo "Binded";
                // Execute the prepared query.
                if (! $insert_stmt->execute()) {
                    die($mysqli->error);
                    // header('Location: ../home.php?msg=1');
                }
                else {
                    // Success, forward
                    header('Location: ../home.php?msg=2');
                }
            }
            else {
                die($mysqli->error);
            }

删除原始脚本中此部分底部的 header('Location: ../home.php?msg=2');

关于PHP - 安全更改密码表单/方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27047687/

相关文章:

mysql - 从数据库初始化一维数组

php - 使用 PHP 检查 MySQL NULL 值

php - 我可以在计算机关闭时运行 mySQL 吗?

php - 播放列表项批量插入 YouTube API v3

php - CSS 在图像周围环绕文字

用于密码验证的php

mysql - 查找 AM Times 并更新?

php PDO 更新语句 : which is safer?

php - 运行以下 PHP/MySQL/Linux 系统所需的硬件

php - MySQL数据库-容量规划困境,请指教