使用PHP和函数更改MySQLi表中的密码

时间:2014-05-11 19:11:52

标签: php mysql forms mysqli

我今天有一个健忘的时间,并且在几年内没有完全恢复编码,所以需要一些帮助!我正在尝试创建一个更改密码表单来验证会话数据(我已经完成)的登录,并让用户通过输入两次新密码(一个用于确认)然后更新MySQL表来更改密码。

我遇到的问题是我的更改密码功能在另一个文件中,我忘了如何将其与不同页面上的表单链接;如果我应该使用post或get?

我得到的代码是:

<form method="post" action="reset_password">
Password: <input type="password" name="password1" id="password1"/></br>
Confirm Password: <input type="password" name="password2" id="password2/></br>
            <input type="button" value="Change">
            <input type="submit" value="Change Password">

到目前为止更改密码位;我需要添加一些从我的functions.php调用函数的方法,并确认写的密码是相同的。

在我的functions.php中我写了

function reset_password($mysqli) {
    if (isset($_SESSION['user_id'], 
              $_SESSION['username'], 
              $_SESSION['login_string'])) {

             $user_id = $_SESSION['user_id'];
        $login_string = $_SESSION['login_string'];
            $username = $_SESSION['username'];
        $user_browser = $_SERVER['HTTP_USER_AGENT'];

        if ($stmt = $mysqli->prepare("SELECT password, salt 
                                      FROM login_secure 
                                      WHERE id = ? LIMIT 1")) {
            // Bind "$user_id" to parameter. 
            $stmt->bind_param('i', $user_id);
            $stmt->execute();   // Execute the prepared query.
            $stmt->store_result();

            if ($stmt->num_rows == 1) {
                // If the user exists get variables from result.
                $stmt->bind_result($password, $salt);
                $stmt->fetch();
                $login_check = hash('sha512', $password . $user_browser);

                if ($login_check == $login_string) {
                    $new_password = hash('sha512', $password1 . $salt);
                    $insert_stmt = $mysqli->prepare("UPDATE login_secure SET password = ".$new_password." WHERE id = ".$user_id."");
                    $insert_stmt->execute();
                }}}}}

我还没有能够测试它,因为我没想过如何将它们连接在一起。有没有人有任何线索,我的思想正在老去;并且空白了。

1 个答案:

答案 0 :(得分:-1)

<form method="POST" action="functions.php">
Password: <input type="password" name="password1" id="password1" /><br>
Confirm Password: <input type="password" name="password2" id="password2" /><br>
        <input type="submit" value="Change Password" />
</form>

这是functions.php的代码

<?php
session_start();
$user_id = $_SESSION['user_id'];
$password1 = $_POST['password1'];
$password2 = $_REQUEST['password2'];

include('../database_connection.php');
$sql = mysqli_query($connection, "SELECT password, salt 
                                  FROM login_secure WHERE id ='".$user_id."'");
while($row = mysqli_fetch_array($sql)){ $salt = $row['salt'];
$password = $password1;
$hash = md5($salt . $password);

mysqli_query($connection, "UPDATE login_secure SET password = '".$hash."' WHERE id='".$user_id."'");
}
?>