<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Checks if the given hash matches the given options</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.password-hash.html">password_hash</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.password-verify.html">password_verify</a></div> <div class="up"><a href="ref.password.html">Password Hashing Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.password-needs-rehash" class="refentry"> <div class="refnamediv"> <h1 class="refname">password_needs_rehash</h1> <p class="verinfo">(PHP 5 >= 5.5.0, PHP 7)</p><p class="refpurpose"><span class="refname">password_needs_rehash</span> — <span class="dc-title">Checks if the given hash matches the given options</span></p> </div> <div class="refsect1 description" id="refsect1-function.password-needs-rehash-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><strong>password_needs_rehash</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$hash</code></span> , <span class="methodparam"><span class="type">int</span> <code class="parameter">$algo</code></span> [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$options</code></span> ] )</div> <p class="para rdfs-comment"> 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. </p> </div> <div class="refsect1 parameters" id="refsect1-function.password-needs-rehash-parameters"> <h3 class="title">Parameters</h3> <dl> <dt> <code class="parameter">hash</code></dt> <dd> <p class="para"> A hash created by <span class="function"><a href="function.password-hash.html" class="function">password_hash()</a></span>. </p> </dd> <dt> <code class="parameter">algo</code></dt> <dd> <p class="para"> A <a href="password.constants.html" class="link">password algorithm constant</a> denoting the algorithm to use when hashing the password. </p> </dd> <dt> <code class="parameter">options</code></dt> <dd> <p class="para"> An associative array containing options. See the <a href="password.constants.html" class="link">password algorithm constants</a> for documentation on the supported options for each algorithm. </p> </dd> </dl> </div> <div class="refsect1 examples" id="refsect1-function.password-needs-rehash-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-995"> <p><strong>Example #1 Usage of <span class="function"><strong>password_needs_rehash()</strong></span></strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$password </span><span style="color: #007700">= </span><span style="color: #DD0000">'rasmuslerdorf'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$hash </span><span style="color: #007700">= </span><span style="color: #DD0000">'$2y$10$YCFsG6elYca568hBi2pZ0.3LDL5wjgxct1N8w/oLR/jfHsiQwCqTS'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// The cost parameter can change over time as hardware improves<br /></span><span style="color: #0000BB">$options </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'cost' </span><span style="color: #007700">=> </span><span style="color: #0000BB">11</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Verify stored hash against plain-text password<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">password_verify</span><span style="color: #007700">(</span><span style="color: #0000BB">$password</span><span style="color: #007700">, </span><span style="color: #0000BB">$hash</span><span style="color: #007700">)) {<br /> </span><span style="color: #FF8000">// Check if a newer hashing algorithm is available<br /> // or the cost has changed<br /> </span><span style="color: #007700">if (</span><span style="color: #0000BB">password_needs_rehash</span><span style="color: #007700">(</span><span style="color: #0000BB">$hash</span><span style="color: #007700">, </span><span style="color: #0000BB">PASSWORD_DEFAULT</span><span style="color: #007700">, </span><span style="color: #0000BB">$options</span><span style="color: #007700">)) {<br /> </span><span style="color: #FF8000">// If so, create a new hash, and replace the old one<br /> </span><span style="color: #0000BB">$newHash </span><span style="color: #007700">= </span><span style="color: #0000BB">password_hash</span><span style="color: #007700">(</span><span style="color: #0000BB">$password</span><span style="color: #007700">, </span><span style="color: #0000BB">PASSWORD_DEFAULT</span><span style="color: #007700">, </span><span style="color: #0000BB">$options</span><span style="color: #007700">);<br /> }<br /><br /> </span><span style="color: #FF8000">// Log user in<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.password-needs-rehash-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <strong><code>TRUE</code></strong> if the hash should be rehashed to match the given <code class="parameter">algo</code> and <code class="parameter">options</code>, or <strong><code>FALSE</code></strong> otherwise. </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.password-hash.html">password_hash</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.password-verify.html">password_verify</a></div> <div class="up"><a href="ref.password.html">Password Hashing Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>