如何检查用户名和密码是否与数据库值匹配

Jan*_*uka 3 php mysql passwords php-password-hash

如果这个问题看起来很傻,我真的很抱歉.但是我一直在努力检查我的数据库usernamepassword在数据库中匹配我在html页面中输入的内容...这是我的登录表单...

<form method="POST" action="Dashboard/Dashboard.php">

    <div class="form-group md-form">
        <!--<input type="email" class="form-control" id="email" value="" placeholder="Enter email address">-->
        <i class="fa fa-user prefix grey-text"></i>
        <input name="username" id="username" type="text" class="form-control" required>
        <label for="defaultForm-email">Username</label>
    </div>
    <div class="form-group md-form">
        <!--<input type="password" class="form-control" id="password" value="" placeholder="Enter password">-->
        <i class="fa fa-lock prefix grey-text"></i>
        <input name="password" id="password" type="password"  class="form-control" required>
        <label for="defaultForm-pass">Your password</label>
    </div>
    <div class="text-center">
        <button type="reset" class="btn btn-amber btn-sm"><strong>Reset</strong></button>
        <input type="submit" name="submit" id="submit" class="btn btn-green btn-sm" value="Sign in">                                           
    </div>

</form>
Run Code Online (Sandbox Code Playgroud)

这是php我正在使用的代码()Dashboard.php

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $databaseName = "test";

    $conn = mysqli_connect($servername, $username, $password, $databaseName);

    $un = $_POST['username'];
    $pw = $_POST['password'];
    print $pass . "_" . $email;

    $query = mysqli_query($conn, "SELECT log_username,log_password FROM login WHERE log_username='$un' AND log_password='$pw'");

    $result_can = mysqli_query($conn, $query);


    while ($row = mysql_fetch_assoc($result_can)) {


        $check_username = $row['username'];
        $check_password = $row['password'];
    }
    if ($un == $check_username && $pw == $check_password) {
        $message = "ok";
        echo "<script type='text/javascript'>alert('$message');</script>";
        header("Location: Doctors.php");
    } else {
        $message = "No";
        echo "<script type='text/javascript'>alert('$message');</script>";
        header("Location: Doctors.php");
    }
    ?>
Run Code Online (Sandbox Code Playgroud)

我真的尝试了几千次,但无法弄清楚我哪里出错了...有谁能帮助我?

我知道我的代码是开放的SQL注入,但我不关心它,因为这是我需要向我的朋友展示的一个例子所以忽略那一部分.

O. *_*nes 7

Stack Overflow适用于"专业和发烧友程序员".尊敬的是,你已经向我们展示了你的问题中的代码,这些代码甚至不值得任何一个名字.

StackOverflow人员对糟糕的安全代码没有太大的幽默感.你得到像你这样的代码的强烈反应,因为,Equifax,Ashley Madison和Adobe,以及被网络犯罪分子破解的所有其他地方.我们为什么要跳上你?因为我们不喜欢网络犯罪分子,我们不想让他们生活轻松.朋友不要让朋友做错密码安全.朋友不要向朋友展示严重不安全的密码验证码.

您的代码有什么问题?您将密码存储为纯文本,并且您很容易受到SQL注入攻击.我将解决第一个问题.

幸运的是,php具有良好的密码安全性.在这里阅读它们. http://php.net/manual/en/faq.passwords.php 使用它们.你如何处理密码?

  1. 当用户在您的网站上注册并首先显示密码时,您可以将其哈希,就像这样.
  $usersPassword = $_POST['password']);
  $hash = password_hash( $usersPassword , PASSWORD_DEFAULT );
  // you then store the username and the hash in your dbms. 
  // the column holding the hash should be VARCHAR(255) for future-proofing
  // NEVER! store the plain text (unhashed) password in your database
Run Code Online (Sandbox Code Playgroud)
  1. 当用户尝试登录时,您执行以下查询:

    SELECT log_password FROM log_user WHERE log_username = TheUsernameGiven
    
    Run Code Online (Sandbox Code Playgroud)

    然后,将检索到的密码放入名为的列中$hash.

    然后使用php的password_verify()函数检查您的用户刚给您的密码是否与数据库中的密码匹配.

    最后,检查是否需要重新设置用户的密码,因为之前用于散列的方法已经过时.

 $usersPassword = $_POST['password']);
 $valid = password_verify ( $usersPassword, $hash );
 if ( $valid ) {
   if ( password_needs_rehash ( $hash, PASSWORD_DEFAULT ) ) {
     $newHash = password_hash( $usersPassword, PASSWORD_DEFAULT );
     /* UPDATE the user's row in `log_user` to store $newHash */
   }
   /* log the user in, have fun! */
 }
 else {
  /* tell the would-be user the username/password combo is invalid */
 }
Run Code Online (Sandbox Code Playgroud)

这个序列是未来的,因为如果旧的哈希方法太容易让网络蠕变破解,它可以在以后重新密码.