array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'uk', ), 'this' => array ( 0 => 'function.password-needs-rehash.php', 1 => 'password_needs_rehash', 2 => 'Checks if the given hash matches the given options', ), 'up' => array ( 0 => 'ref.password.php', 1 => 'Функції Хешування Пароля', ), 'prev' => array ( 0 => 'function.password-hash.php', 1 => 'password_hash', ), 'next' => array ( 0 => 'function.password-verify.php', 1 => 'password_verify', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/password/functions/password-needs-rehash.xml', ), 'history' => array ( ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); contributors($setup); ?>

password_needs_rehash

(PHP 5 >= 5.5.0, PHP 7, PHP 8)

password_needs_rehashChecks if the given hash matches the given options

Опис

password_needs_rehash(string $hash, string|int|null $algo, array $options = []): bool

This function checks to see if the supplied hash implements the algorithm and options provided. If not, it is assumed that the hash needs to be rehashed.

Параметри

hash

Геш, створений функцією password_hash().

algo

Константа алгоритму для паролю, що позначає алгоритм для гешування пароля.

options

Асоціативниий масив опцій. Документація щодо доступних опцій для кожного алгоритму викладена на сторінці Константи алгоритмів для паролів.

Значення, що повертаються

Returns true if the hash should be rehashed to match the given algo and options, or false otherwise.

Журнал змін

Версія Опис
7.4.0 The algo parameter expects a string now, but still accepts ints for backward compatibility.

Приклади

Приклад #1 Usage of password_needs_rehash()

<?php

$password
= 'rasmuslerdorf';
$hash = '$2y$12$4Umg0rCJwMswRw/l.SwHvuQV01coP0eWmGzd61QH2RvAOMANUBGC.';

$algorithm = PASSWORD_BCRYPT;
// bcrypt's cost parameter can change over time as hardware improves
$options = ['cost' => 13];

// Verify stored hash against plain-text password
if (password_verify($password, $hash)) {
// Check if either the algorithm or the options have changed
if (password_needs_rehash($hash, $algorithm, $options)) {
// If so, create a new hash, and replace the old one
$newHash = password_hash($password, $algorithm, $options);

// Update the user record with the $newHash
}

// Perform the login.
}
?>