我想知道如何生成密码哈希?
// This is my code:
$email="mail@example.net";
$password="mypassword";
// How to get password_hash variable?
$user = User::find()->where(['email'=>$email, 'password_hash'=>$password_hash])->one();
if(isset($user)){
echo "there is";
} else {
"Sorry!";
}
谢谢。
答案 0 :(得分:2)
http://www.yiiframework.com/doc-2.0/guide-security-passwords.html Yii如何处理密码2.除非你是加密专家,否则不要试着写自己的密码。
答案 1 :(得分:0)
public function verifyPassword($password) { if(md5($password) === $this->password) return TRUE; else return FALSE; //return Yii::$app->security->validatePassword($password, $this->password); } public function beforeSave($insert) { // hash new password if set if ($this->newPassword) { //$this->password = Yii::$app->security->generatePasswordHash($this->newPassword); $this->password = md5($this->newPassword); } // convert ban_time checkbox to date if ($this->ban_time) { $this->ban_time = date("Y-m-d H:i:s"); } // ensure fields are null so they won't get set as empty string $nullAttributes = ["email", "username", "ban_time", "ban_reason"]; foreach ($nullAttributes as $nullAttribute) { $this->$nullAttribute = $this->$nullAttribute ? $this->$nullAttribute : null; } return parent::beforeSave($insert); }
答案 2 :(得分:-4)
我在md5中使用哈希,但我在用户模型中创建了函数
public function validatePassword($password)
{
return $this->PASSWORD === md5($password);
}