password_needs_rehash

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

password_needs_rehashChecks if the given hash matches the given options

Description

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.

Parameters

hash

A hash created by password_hash().

algo

A password algorithm constant denoting the algorithm to use when hashing the password.

options

An associative array containing options. See the password algorithm constants for documentation on the supported options for each algorithm.

Return Values

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

Changelog

Version Description
7.4.0 The algo parameter expects a string now, but still accepts ints for backward compatibility.

Examples

Example #1 Usage of password_needs_rehash()

<?php

$password 
'rasmuslerdorf';
$hash '$2y$10$YCFsG6elYca568hBi2pZ0.3LDL5wjgxct1N8w/oLR/jfHsiQwCqTS';

// The cost parameter can change over time as hardware improves
$options = array('cost' => 11);

// Verify stored hash against plain-text password
if (password_verify($password$hash)) {
    
// Check if a newer hashing algorithm is available
    // or the cost has changed
    
if (password_needs_rehash($hashPASSWORD_DEFAULT$options)) {
        
// If so, create a new hash, and replace the old one
        
$newHash password_hash($passwordPASSWORD_DEFAULT$options);
    }

    
// Log user in
}
?>

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial