简体   繁体   English

使用bCrypt哈希密码登录PHP

[英]PHP Login with bCrypt Hashed Passwords

Why didn't the other posts help? 为什么其他帖子没有帮助?

Other posts didn't help because one was asking since none worked, despite there are a lot of similar questions. 其他帖子无济于事,因为尽管有很多类似的问题,但由于没有一个工作而一直在问。

More details 更多细节

The thing I want to do is to make a user log in using his password. 我要做的是使用户使用其密码登录。 But, the passwords are hashed using bCrypt in the database. 但是,密码是使用数据库中的bCrypt进行哈希处理的。

When I try to enter the real password, it doesn't work and says that the password is incorrect. 当我尝试输入真实密码时,它不起作用,并提示密码错误。

But, when I try to enter the hashed password. 但是,当我尝试输入哈希密码时。 It says: "Successfully logged in". 它说:“成功登录”。

How to make it log in using the real password not the hash?! 如何使用真实密码而不是哈希登录?

Code

Login.php Login.php

 <form method="post" action="loginsession.php">//login <p>Username <input type="text" name="uid" size="20"> </p> <p>Password <input type="password" name="pwd" size="20"> </p> <p><input type="submit" value="Login" name="login"></p> </form> 

Loginsession.php Loginsession.php

  <?php session_start(); include ('dbhandler.php'); $uid = $_POST['uid']; $pwd = $_POST['pwd']; $sql = "SELECT * FROM user WHERE uid='$uid' and pwd='$pwd' "; $result = mysqli_query($conn, $sql); $row = mysqli_fetch_assoc($result); $encrypted_pwd = password_hash($pwd, PASSWORD_DEFAULT); $hash = password_verify($pwd,$encrypted_pwd); $count = mysqli_num_rows($result); if ($count == 1) { echo("Logging in..."); $_SESSION['id'] = $row['id']; $_SESSION['uid'] = $row['uid']; $_SESSION['pwd'] = $row['pwd']; echo("<h1 style='color:green;'>Successfully Logged In"); } else { echo "Your Login Name or Password is invalid"; die(); } ?> 

There was so much wrong that a rewrite was more appropriate: 发生了太多错误,因此重写更为合适:

<?php 
   session_start(); 
   include 'dbhandler.php'; // shouldn't be include './dbhandler.php'; ? 
   $uid = $_POST['uid']; 
   $pwd = $_POST['pwd']; 

   $sql = "SELECT * FROM user WHERE uid='".mysqli_real_escape_string($conn, $uid)."'"; 
   $result = mysqli_query($conn, $sql);
   if (mysqli_num_rows($result) === 1) {
     $row = mysqli_fetch_assoc($result);
     if (password_verify($pwd, $row['pwd'])) {
       $_SESSION['id'] = $row['id']; 
       $_SESSION['uid'] = $row['uid']; 
       $_SESSION['pwd'] = $row['pwd'];
       // redirect to "login success" page would be a better solution
       echo "<h1 style='color:green;'>Successfully Logged In";
     } else {
       echo "Invalid password";
     }
   } else {
     echo "Your login name is invalid";
   }
  1. You should never use textual user input as a parameter of your query. 您永远不应将文本用户输入用作查询的参数。 This leaves you vulnerable to SQL injection . 这使您容易受到SQL注入的攻击。 To prevent that, either use PDO or mysqli_* (but in this case use the appropriate functions to escape the parameters), which are both well documented. 为避免这种情况,请使用PDOmysqli _ * (但在这种情况下,请使用适当的函数来转义参数),这两个文件均已记录在案。

  2. You define and execute the query with passing the password as it was. 您可以通过传递密码来定义和执行查询。 You need to hash the password BEFORE you pass it to the query and pass $encrypted_pwd to the query. 您需要先对密码进行哈希处理,然后再将其传递给查询,并将$encrypted_pwd传递给查询。

  3. I recommend the usage of limit 0, 1 at the end of your query in this case, as that will specify that you are interested only in the very first corresponding row, if exists, so the query will be stopped if the user is found and not continue searching for users. 在这种情况下,我建议在查询的末尾使用limit 0, 1因为这将指定您仅对第一个对应的行(如果存在)感兴趣,因此,如果找到用户并停止查询,则查询将停止不要继续搜索用户。 This will optimize your query. 这将优化您的查询。

  4. Since you do not want to load any particular data related to the user, you only want to find out whether the user exists, you could further optimize your query by replacing select * with select 1 , which will significantly reduce the size of data your RDBMS will have to send in response, as only a number will be returned and not the whole record, possibly with the user's biography. 由于您不想加载与该用户有关的任何特定数据,因此只想查找该用户是否存在,因此可以通过将select *替换为select 1来进一步优化查询,这将大大减少RDBMS的数据量将会发送响应,因为只会返回一个数字,而不是整个记录,可能会返回用户的传记。

  5. Never ever store a user's password in the session. 永远不要在会话中存储用户密码。 If for some reason $_SESSION will be outputted to the browser, that will be a serious vulnerability. 如果由于某种原因$_SESSION将输出到浏览器,则将是一个严重的漏洞。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM