问:我正在尝试创建不在数据库中的随机 5 位数字作为 PASSWORD_HASH。
这是我试图做的一个例子:
function check($num, $pwdcheck) {
if(password_verify($num, $pwdcheck)) {
return "exist";
} else {
return $num;
}
}
function createLoginPassword() {
/* Passwords from database */$pwds = array('$2y$10$gPenW2YPLzoZOKb/PZ8SC.UFh6C0cLALoO11x/8hjP3GeefMJ6sOu' /*<- Number 1*/, '$2y$10$iOOmAx4kJLNP5H91tfoaz.SarIA1byrUgEE8rtt9llqth5l4v5ACC' /*<- Number 2*/);
foreach ($pwds as $result) {
do {
$output_password = rand(0,4);
} while (check($output_password, $result) == "exist");
$out = $output_password;
}
return $out;
}
echo createLoginPassword();
但它似乎不起作用,它不会生成不在数组中的唯一数字
答:试试这个
function createLoginPassword() {
// Passwords from database
$pwds = [
'$2y$10$gPenW2YPLzoZOKb/PZ8SC.UFh6C0cLALoO11x/8hjP3GeefMJ6sOu' /*<- Number 1*/,
'$2y$10$iOOmAx4kJLNP5H91tfoaz.SarIA1byrUgEE8rtt9llqth5l4v5ACC' /*<- Number 2*/
];
// while password exist, generate new one
do {
// generate 5-digit number
$output = rand(10000, 99999);
} while( !empty(array_filter($pwds, fn($pwd) => password_verify($output, $pwd))) );
return $output;
}
echo createLoginPassword();