yii\base\Security::validatePassword PHP Méthode

validatePassword() public méthode

Verifies a password against a hash.
See also: generatePasswordHash()
public validatePassword ( string $password, string $hash ) : boolean
$password string The password to verify.
$hash string The hash to verify the password against.
Résultat boolean whether the password is correct.
    public function validatePassword($password, $hash)
    {
        if (!is_string($password) || $password === '') {
            throw new InvalidParamException('Password must be a string and cannot be empty.');
        }
        if (!preg_match('/^\\$2[axy]\\$(\\d\\d)\\$[\\.\\/0-9A-Za-z]{22}/', $hash, $matches) || $matches[1] < 4 || $matches[1] > 30) {
            throw new InvalidParamException('Hash is invalid.');
        }
        if (function_exists('password_verify')) {
            return password_verify($password, $hash);
        }
        $test = crypt($password, $hash);
        $n = strlen($test);
        if ($n !== 60) {
            return false;
        }
        return $this->compareString($test, $hash);
    }

Usage Example

Exemple #1
0
 /**
  * Validates password
  *
  * @param  string $password password to validate
  * @return boolean if password provided is valid for current user
  */
 public function validatePassword($password)
 {
     $security = new Security();
     return $security->validatePassword($password, $this->password);
 }