Answer the question
In order to leave comments, you need to log in
Why is the password_verify function buggy?
Hello!
for some reason, the password_verify function does not work correctly
code examples:
creating a hash in the beforeValidate method:
if(in_array($this->scenario, ['userCreate','singUp'])) {
$this->password_hash = $this->setPassword($this->password);
}
public function setPassword($password)
{
return $this->password_hash = Yii::$app->security->generatePasswordHash($password,5);
}
public function validatePassword($attribute, $params)
{
if (!$this->hasErrors()) {
$user = $this->getUser();
if (!$user || !$user->validatePassword($this->password)) {
$this->addError($attribute, 'Неправильный логин или пароль.');
}
}
}
//метод validatePAssword
public function validatePassword($password)
{
return Yii::$app->security->validatePassword($password, $this->password_hash);
}
Answer the question
In order to leave comments, you need to log in
php > var_dump(password_verify(null, '$2y$05$WjKwE1MiWPyPFghASGGcdupFqfcUHchkb2y/rwTiCWe40iIbqyxKq'));
bool(true)
php > var_dump(password_verify('', '$2y$05$WjKwE1MiWPyPFghASGGcdupFqfcUHchkb2y/rwTiCWe40iIbqyxKq'));
bool(true)
in general, a password hash of 12345678 is generated ($2y$05$WjKwE1MiWPyPFghASGGcdupFqfcUHchkb2y/rwTiCWe40iIbqyxKq)
<?php $password = '12345678';
var_dump(password_verify($password, '$2y$05$WjKwE1MiWPyPFghASGGcdupFqfcUHchkb2y/rwTiCWe40iIbqyxKq'));
// bool(false)
var_dump(password_verify($password, password_hash($password, PASSWORD_DEFAULT, ['cost' => 5])));
// bool(true)
Good evening.
What do you want to achieve in the beforeValidate() method?
In the first validatePassword(), as I understand it in the LoginForm model, it is not necessary to pass parameters.
public function validatePassword()
{
if (!$this->hasErrors()) {
$user = $this->getUser();
if (!$user || !$user->validatePassword($this->password)) {
$this->addError('password', 'Неправильный логин или пароль.');
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question