【问题标题】:Password hashing not working in php mysql密码散列在 php mysql 中不起作用
【发布时间】:2016-07-06 14:10:29
【问题描述】:

我正在尝试使用 phpmysql 使用密码散列。问题是 password_verify 到目前为止似乎对我不起作用。比如说,我注册时的密码是“123456789”。我使用

将它存储在数据库中
    password_hash('123456789', PASSWORD_BCRYPT, array('cost' => 12));

然后当我在登录字段中输入“123456789”时,它什么也不做,失败了。

这是我的代码:

<?php
        session_start();
        include('db.php');        
?>

<!DOCTYPE html>

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1" />
    <link rel="stylesheet" type="text/css" href="style.css"/>

</head>

<body>

<p/>

<?php

    if(isset($_POST['login']) && $_POST['login'] == 'Login') {

        $loginEmail = $_POST['loginEmail'];
        $loginPassword = $_POST['loginPassword'];

        $sqlLogin = $db->prepare("SELECT * FROM registered_users WHERE email = ?");

        $sqlLogin->bind_param("s",$loginEmail);
        $sqlLogin->execute();
        $sqlLogin = $sqlLogin->get_result();
        $numrowsLogin = $sqlLogin->num_rows;

        if($numrowsLogin == 1) {
            $rowLogin = $sqlLogin->fetch_assoc(); 
            $stored_password = $rowLogin['password'];

        }
        if(password_verify($loginPassword, $stored_password)){


           header('Location: homepage.php'); 
        }else{
            echo 'invalid login';
        }      

    }         
?>


    <form action = "<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
        <table style="width:500px">                        
            <tr>
                <td width="30%"><input style="width: 200px; height: 25px; border-radius: 5px;" type="text" name="loginEmail" placeholder = "Email" required/><br/></td>
            </tr>                    
            <tr>
                <td width="30%"><input style="width: 200px; height: 25px; border-radius: 5px;" type="password"  name="loginPassword" placeholder = "Password" required/><br/></td>
            </tr>
        </table>

        <input style="font-weight: bold; width: 70px; height: 25px; border-radius: 5px;" type="submit" name="login" value="Login"/>
    </form>

</body>

</html>

【问题讨论】:

  • 是否进行了任何基本调试,例如检查您的查询是否成功并返回存储的哈希?请注意,在表单的 action 中使用 $_SERVER['PHP_SELF'] 很容易受到 XSS 攻击。
  • 您需要在任何header('location: ...');-call 之后添加exit;,因为此时我们要停止向浏览器输出内容。
  • 是的,抱歉,已更新
  • php.net/manual/en/function.password-hash.php $2y$10$.vGA1O9wmRjrwAVXD98HNOgsNpDczlqm3Jq7KnEd1rVAGv3Fykk1a 的示例为 60 个字符。你的密码栏和密码栏的长度是多少?不到60?如果是这样,那就是问题所在。太短了,你的代码会因为它默默地失败,你需要在改变列的长度后重新开始一个新的哈希。 @BishwaroopChakraborty
  • 请阅读@fred 的答案中的 cmets。你再也不会遇到这个问题了。

标签: php mysql password-hash php-password-hash


【解决方案1】:

@Fred Li:谢谢,这对我有用。我在数据库中的密码列长度是 50。更新它并且现在可以使用,再次感谢您! – Bishwaroop Chakraborty”

如评论中所述:

来自http://php.net/manual/en/function.password-hash.php的示例

$2y$10$.vGA1O9wmRjrwAVXD98HNOgsNpDczlqm3Jq7KnEd1rVAGv3Fykk1a 是 60 个字符。

您的密码列的长度小于 60,这就是问题所在。

它太短了,因此您的代码静默失败,您需要在更改列的长度后重新开始使用新的哈希值。

  • 手册上说 255 是一个不错的选择。

注意事项:

注意其他关于 XSS 注入的 cmets。

这里有几篇不错的文章:

并在标题后添加exit;。否则,您的代码可能需要继续执行。

【讨论】:

  • imo,只需使用 255 的 varchar 字段。成本是额外的一个字节,这个错误永远不会发生。 imo,永远不要在数据库中使用精确长度的字符/ varchar 字段。
  • @RyanVincent 完全正确;我写了一个关于手册的简短说明,说这是“一个不错的选择”;-) 谢谢你的评论。
  • 对不起,@fred,我的意思是给 OP ;-/ 手指麻烦
  • @RyanVincent 不用担心 :-)
【解决方案2】:

如果新人在验证足够的数据存储后仍然遇到此错误(例如:varchar255

请务必在验证密码函数中使用未散列的string

verify_password($unhashed-string, $hashed-string)

将散列的string 传递给函数的第一个参数,我失眠了几个小时。

【讨论】:

    【解决方案3】:

    我正在使用河豚算法对密码进行哈希处理。我已经成功运行了,你可以试试这个。

    <?php
    
    $pass = "test678";
    
    $hash = password_hash($pass, PASSWORD_BCRYPT);  //password_hash() function hash given password
    
    $matchpass = "test678";
    
    $match = password_verify($matchpass, $hash); // password_verify() function hash given boolean value if password can match so it return 1 otherwise 0
    
    if ($match == true) {
        echo "Password can match successfully......";
    } else {
        echo "Password cannot match please try again.";
    }
    
    ?>
    

    输出:

    密码可以匹配成功......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-19
      • 2016-06-19
      • 1970-01-01
      • 2019-09-22
      • 2015-08-13
      • 2012-05-30
      • 2011-01-20
      相关资源
      最近更新 更多