W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
(PHP 5 >= 5.5.0, PHP 7, PHP 8)
password_needs_rehash — 檢測散列值是否匹配指定的選項
password_needs_rehash(string $hash, string|int|null $algo, array $options = []): bool
此函數(shù)檢測指定的散列值是否實(shí)現(xiàn)了提供的算法和選項。 如果沒有,需要重新生成散列值。
hash
一個由 password_hash() 創(chuàng)建的散列值。
algo
一個用來在散列密碼時指示算法的密碼算法常量。
options
一個包含有選項的關(guān)聯(lián)數(shù)組。詳細(xì)的參數(shù)說明,請參考文檔 密碼算法常數(shù)。
如果散列需要重新生成才能匹配指定的 algo 和 options, 則返回 true,否則返回 false。
版本 | 說明 |
---|---|
7.4.0 | 現(xiàn)在 algo 參數(shù)可以支持 string 類型,但為了向后兼容性,同時支持 int 類型。 |
示例 #1 password_needs_rehash()用法
<?php
$password = 'rasmuslerdorf';
$hash = '$2y$10$YCFsG6elYca568hBi2pZ0.3LDL5wjgxct1N8w/oLR/jfHsiQwCqTS';
$algorithm = PASSWORD_BCRYPT;
// bcrypt 的成本參數(shù)隨著硬件的改進(jìn)而可能發(fā)生變化
$options = ['cost' => 12];
// 根據(jù)明文密碼驗證儲存的散列
if (password_verify($password, $hash)) {
// 檢查算法或選項是否已經(jīng)更改
if (password_needs_rehash($hash, $algorithm, $options)) {
// 如果是這樣,則創(chuàng)建新散列,替換舊散列
$newHash = password_hash($password, $algorithm, $options);
// 使用 $newHash 更新用戶記錄
}
// 執(zhí)行登錄
}
?>
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: