密码哈希在php mysql中不起作用
我正在尝试使用phpmysql使用密码哈希。问题是到目前为止,密码验证似乎对我不起作用。比如说,我在注册期间的密码是“123456789”。我使用密码哈希在php mysql中不起作用,php,mysql,password-hash,php-password-hash,Php,Mysql,Password Hash,Php Password Hash,我正在尝试使用phpmysql使用密码哈希。问题是到目前为止,密码验证似乎对我不起作用。比如说,我在注册期间的密码是“123456789”。我使用 password_hash('123456789', PASSWORD_BCRYPT, array('cost' => 12)); 然后,当我在登录字段中输入“123456789”时,它什么也不做,失败 这是我的密码: <?php session_start(); include('db.php'
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>
@李华明:谢谢,这对我很有效。数据库中我的密码列长度为50。更新后现在可以使用了,再次感谢!!–Bishwaroop脉轮”
如文中所述:
来自
$2y$10$.vga1o9wmrjrwavxd98hnogsnpdczlqm3jq7kned1rvagv3fykkkk1a
为60个字符
密码列的长度小于60,这就是问题所在
它太短,因此您的代码以静默方式失败,在更改列的长度后,您需要使用新的哈希重新开始
- 手册上说255是个不错的选择
注:
请注意关于XSS注入的其他注释
以下是一些好文章:
并在标题后添加exit;
。否则,您的代码可能需要继续执行
@李华明:谢谢,这对我来说很有效。我在数据库中的密码列长度是50。现在更新并生效了,再次感谢!!–Bishwaroop Chakraborty“
如文中所述:
来自
$2y$10$.vga1o9wmrjrwavxd98hnogsnpdczlqm3jq7kned1rvagv3fykkkk1a
为60个字符
密码列的长度小于60,这就是问题所在
它太短,因此您的代码以静默方式失败,在更改列的长度后,您需要使用新的哈希重新开始
- 手册上说255是个不错的选择
注:
请注意关于XSS注入的其他注释
以下是一些好文章:
添加退出代码>在标题之后。否则,您的代码可能需要继续执行。如果新加入者在验证足够的数据存储后仍然在这方面出错(例如:varchar255
)
确保在验证密码功能中使用未删除的字符串
verify_password($unhashed-string, $hashed-string)
将散列的字符串
传递到函数的第一个参数时,我失去了几个小时的睡眠。如果新加入者在验证足够的数据存储后仍然在这方面出错(例如:varchar255
)
verify_password($unhashed-string, $hashed-string)
确保在验证密码功能中使用未删除的字符串
verify_password($unhashed-string, $hashed-string)
我在把一个散列的字符串传递到函数的第一个参数时睡了几个小时。我正在使用blowfish算法对密码进行散列。它已经为我成功运行,所以您可以尝试这个
verify_password($unhashed-string, $hashed-string)
<?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.";
}
?>
输出:
密码可以成功匹配……我正在使用blowfish算法对密码进行哈希运算。它已经为我成功运行,所以您可以尝试这个
<?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.";
}
?>
输出:
密码可以成功匹配……完成任何基本调试,如检查查询是否成功并返回存储的哈希?请注意,通过在表单的操作中使用$\u服务器['PHP\u SELF']
,您很容易受到XSS攻击。您需要添加退出任何标题('location:…')后的code>代码>-调用,因为此时我们要停止向浏览器输出内容。是的,很抱歉,从$2y$10$更新示例。vga1o9wmrjrwavxd98hnogsnpdczlqm3jq7kned1rvagv3fykkkkk1a
为60个字符。您的密码和密码列的长度是多少?不到60?如果是这样,那就是问题所在。由于太短,代码会以静默方式失败,因此需要在更改列的长度后使用新的哈希重新开始@Bishwaroopchakraborty请阅读@fred回答中的评论。您将永远不会再遇到此问题。是否执行任何基本调试,如检查查询是否成功并返回存储的哈希?请注意,通过在表单的操作中使用$\u服务器['PHP\u SELF']
,您很容易受到XSS攻击。您需要添加退出任何标题('location:…')后的code>代码>-调用,因为此时我们要停止向浏览器输出内容。是的,很抱歉,从$2y$10$更新示例。vga1o9wmrjrwavxd98hnogsnpdczlqm3jq7kned1rvagv3fykkkkk1a
为60个字符。您的密码和密码列的长度是多少?不到60?如果是这样,那就是问题所在。由于太短,代码会以静默方式失败,因此需要在更改列的长度后使用新的哈希重新开始@Bishwaroopchakraborty请阅读@fred回答中的评论。你再也不会有这个问题了。imo,只需使用255的varchar
字段。成本是额外的一个字节,这个错误永远不会发生。在我看来,永远不要在数据库中使用精确长度字符/varchar字段。@RyanVincent;我写了一个关于手册的简短说明,说这是“一个好的赌注”;-)顺便说一句,谢谢你的评论。对不起,@fred,我是说这篇文章;-/手指trouble@RyanVincent不用担心:-)在我看来,只需使用255的varchar
字段。成本是额外的一个字节,这个错误永远不会发生。在我看来,永远不要在数据库中使用精确长度字符/varchar字段。@RyanVincent;我写了一个关于手册的简短说明,说这是“一个好的赌注”;-)顺便说一句,谢谢你的评论。对不起,@fred,我是说这篇文章;-/手指trouble@RyanVincent不用担心:-)