: This article describes how to change the password in the YII Framework. if you are interested in the PHP Tutorial, refer to it.
YII2 implements password modification
Main difficulties:
1. password encryption
The results of YII2 encryption on passwords are different, that is, the encryption results obtained by using the same initial password at different times are different, therefore, we cannot use common methods to verify that the password is correct (the password is encrypted and compared with the password in the database ). YII2 has its own encryption and password verification procedures.
Encrypt $ hash = Yii: $ app-> getSecurity ()-> generatePasswordHash ('20140901']);
Verify Yii: $ app-> getSecurity ()-> validatePassword ('20140901', $ hash), return true or false
2. return information processing
Yii: $ app-> session-> setFlash ('Contact ', 'Old password error'); // Set flash
Return $ this-> goBack (); // page jump
Yii: $ app-> session-> getFlash ('contact'); // Obtain The flash
Publicfunction actionPassword () {$ model = new PasswordForm; $ request = YII: $ app-> request; if ($ request-> isPost) {$ p = $ request-> post ('passwordform'); $ id = YII: $ app-> user-> id; $ admin = Admin :: findIdentity ($ id); $ password = $ admin-> password; if (Yii: $ app-> getSecurity ()-> validatePassword ($ p ['password'], $ password) {if ($ p ['pass1'] = $ p ['passs2']) {$ newPass = Yii ::$ app-> getSecurity () -> generatePasswordHash ($ p ['pass1']); $ connection = \ Yii: $ app-> db; $ r = $ connection-> createCommand () -> update ('admin', ['password' => $ newPass], 'id = '. $ id)-> execute (); if ($ r) {Yii ::$ app-> user-> logout (); return $ this-> goHome ();} else {return $ this-> goBack () ;}} else {Yii: $ app-> session-> setFlash ('Contact ', 'Old password error '); return $ this-> redirect (array ('site/password');} else {return $ this-> render ('password ', ['model' => $ model]);}
Code Optimization: The main optimization details are as follows:
1. move the main logic code in the controller into the model.
2. optimize database operations
Controller:
publicfunction actionPassword(){ $model=new PasswordForm; $request = YII::$app->request; if($request->isPost && $model->load(Yii::$app->request->post()) && $model->changePassword()){ Yii::$app->user->logout(); return$this->goHome(); }else{ return$this->render('password',['model'=>$model]); } }
In the model:
Publicfunction changePassword () {$ id = YII: $ app-> user-> id; $ admin = Admin: findIdentity ($ id ); $ password = $ admin-> password; if (Yii: $ app-> getSecurity ()-> validatePassword ($ this-> password, $ password )) {if ($ this-> pass1 ==$ this-> pass2) {$ newPass = Yii ::$ app-> getSecurity () -> generatePasswordHash ($ this-> pass1); $ admin-> password = $ newPass; if ($ admin-> save () {returntrue;} else {returnfalse ;}} else {Yii ::$ app-> session-> setFlash ('Contact ', 'two new passwords are not equal'); returnfalse ;}} else {Yii :: $ app-> session-> setFlash ('Contact ', 'Old password error'); returnfalse ;}}
The above introduces the implementation of password modification under the YII Framework, including the content, hope to be helpful to friends who are interested in the PHP Tutorial.