gpt4 book ai didi

php - 尽管密码正确,password_verify 密码始终无效

转载 作者:行者123 更新时间:2023-11-29 19:12:56 25 4
gpt4 key购买 nike

我不知道我的代码有什么问题hash.php(插入加密密码)

**<?php
$con = new mysqli("localhost", "root", "", "hast") or die(mysqli_error());
if (array_key_exists("f5", $_GET)) {
$w5 = $_GET['f5'];//pass
}
if (array_key_exists("f6", $_GET)) {
$w6 = $_GET['f6'];//pass
}
$salt = md5(uniqid(rand()));
$options = [
'cost' =>11,
'salt' => $salt
];
$hash_password = password_hash($w6, PASSWORD_BCRYPT, $options)."\n";
$sql = mysqli_query($con, "INSERT INTO `pass`(`nama`, `hash_password`, `salt`) VALUES ('$w5','$hash_password','$salt')")or die(mysqli_error($con));
if ($sql) {
echo $hash_password;
} else {
echo "gagal";
}
?>**

hashlog.php

**<?php
$con = new mysqli("localhost", "root", "", "hast") or die(mysqli_error());
if (array_key_exists("f5", $_GET)) {
$w5 = $_GET['f5'];//user
}
if (array_key_exists("f6", $_GET)) {
$w6 = $_GET['f6'];//pass
}
$sql = mysqli_query($con, "select hash_password from pass where nama='$w5'")or die(mysqli_error($con));
$row = mysqli_fetch_assoc($sql);
$hash = $row['hash_password'];
$hash = $row['hash_password'];
//$hash ='$2y$11$0be5c43957cd3df608521u4PiYrUUyK/dQRSlc/g5UVdDdKk1WChy';
if (password_verify($w6, $hash)) {
echo 'Password is valid!';
} else {
echo 'Invalid password.';
}
?>**

就我而言,尽管密码正确,但密码始终无效请帮助我

最佳答案

问题是您指定了无效的salt 值。您不应该自己指定salt,只需让库为您生成一个即可。如果您确实想指定salt,请使用如下代码来执行此操作:

$salt = mcrypt_create_iv(22, MCRYPT_DEV_URANDOM);

此外,我认为您的问题是在哈希密码处附加了 \n ;您必须将其删除:

$hash_password = password_hash($w6, PASSWORD_BCRYPT, $options)."\n"; //remove this "\n"

关于php - 尽管密码正确,password_verify 密码始终无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42945269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com