更新数据库时$ _POST变量不编码为MD5哈希

时间:2015-01-12 02:58:19

标签: php mysql hash md5

我这样做是为了测试我的能力的概念证明,我知道MD5不能安全地用于密码。

我有一个登录页面,可以通过创建输入密码的MD5并根据存储在数据库中的值检查哈希值来正常工作。

我创建了一个页面,用户可以在其中更改用户名(电子邮件地址),以便首先进行此操作:

  1. 该用户的ID位于$ _SESSION [id]变量中。这是使用UPDATE SQL命令发送的,以确保更改正确的用户电子邮件地址。
  2. 必须重新输入密码才能进行额外验证。这不是问题,但是值是纯文本而不是MD5,因此UPDATE SQL命令失败并更新了0行。我使用过md5($ _ POST ['密码'](见第17行代码)

    <?php
    require_once __DIR__ . ('/../config/init.php');
    if($_SESSION['login'] != 1)
    {
        header('Location: /login/');
    
    }
        if($_POST){
    
        $mysqli = new mysqli($db['hostname'], $db['username'], $db['password'], $db['database']);
    
        /* check connection */
        if ($mysqli->connect_errno) {
            printf("Connect failed: %s\n", $mysqli->connect_error);
            exit();
        }
        $result = $mysqli->query("UPDATE users SET email = '" . mysqli_real_escape_string($mysqli, $_POST['email']) . "' WHERE password = '" . mysqli_real_escape_string($mysqli, md5($_POST['password'])) . "' AND id = '" . $_SESSION['id'] . "'");
    
        if($result == 1)
        {
            echo "Email successfully updated. Please attempt to log in with your existing password";
            print_r($_POST);
            print_r($_SESSION);
            echo md5($_POST['password']);
        }
        else{
            echo "epic fail";
    
        }
    }
    ?>
    <!DOCTYPE html>
    <html>
    <head>
    <?php 
        include INCLUDES . 'head_tags.php';
    ?>
    </head>
    <body>
    <div class='container'>
        <?php
            include INCLUDES . 'header.php';
            include INCLUDES . 'nav.php';
        ?>
        <div class='two-thirds column'>
            <h2>User control panel</h2>
            <p>Welcome to the secure page <?php echo $_SESSION['forename'], " ", $_SESSION['surname'];?></p>
            <p>You are currently registered with email address <?php echo $_SESSION['email'];?></p>
    
            <h2>Change user details</h2>
            <p>If you would like to change your email address. Please complete the form below</p>
    
            <form action='#' method='post'>
            <div>
            <label for="email">E-mail address:</label>
            <input type="email" id="email" name="email" placeholder="Email address" autocomplete="on" required>
            <label for="password">Password:</label>
            <input type="password" id="password" name="password" placeholder="Existing Password" autocomplete="on" required>
            </div>
    
            <input type='submit' value='submit'>
            </form>
    
    
        </div>
    </div>
    <?php
        include INCLUDES . 'footer.php';
    ?>
    

  3. 这些行通常不在代码中,但我一直在尝试调试。

    print_r($_POST);
    print_r($_SESSION);
    echo md5($_POST['password']) 
    

    在print_r($ _ POST)上,它是数组中的纯文本 回显$ _POST [&#39;密码&#39;],因为md5将密码作为MD5返回。它与数据库中的内容完全匹配。

    任何帮助都会很棒:)。

0 个答案:

没有答案