YII框架下实现密码修改,yii框架密码修改_PHP教程

WBOY
풀어 주다: 2016-07-13 09:46:25
원래의
1108명이 탐색했습니다.

YII框架下实现密码修改,yii框架密码修改

<span><strong>YII2 实现修改密码功能</strong></span><br /><br /><strong><span>主要难点:</span></strong><br /><br /><span>1、密码加密</span><br /><span>  YII2对密码加密生成的结果是不同的,即用相同的初始密码在不同时间得到的加密结果不同,所以我们不能用常用的方法去验证密码是否正确(将密码加密后与数据库中的密码相比较)。YII2有自己的加密以及密码验证流程。</span><br /><span>  加密 $hash = Yii::$app->getSecurity()->generatePasswordHash('123456']);</span><br /><span>  验证 Yii::$app->getSecurity()->validatePassword('123456', $hash),返回true或false</span><br />2、返回信息处理<br />  Yii::$app->session->setFlash('contact','旧密码错误');//设置 flash<br />  return $this->goBack();                 //页面转跳 <br />  Yii::$app->session->getFlash('contact');       //获取 flash  
로그인 후 복사
<span>public</span> <span>function</span><span> actionPassword(){
        </span><span>$model</span>=<span>new</span><span> PasswordForm;
        </span><span>$request</span> = YII::<span>$app</span>-><span>request;

        </span><span>if</span>(<span>$request</span>-><span>isPost){
            </span><span>$p</span> = <span>$request</span>->post('PasswordForm'<span>);
            </span><span>$id</span> = YII::<span>$app</span>->user-><span>id;
            </span><span>$admin</span>=  Admin::findIdentity(<span>$id</span><span>);
            </span><span>$password</span> = <span>$admin</span>-><span>password;
            </span><span>if</span>(Yii::<span>$app</span>->getSecurity()->validatePassword(<span>$p</span>['password'], <span>$password</span><span>)){
                </span><span>if</span>(<span>$p</span>['pass1'] == <span>$p</span>['pass2'<span>]){
                    </span><span>$newPass</span> = Yii::<span>$app</span>->getSecurity()->generatePasswordHash(<span>$p</span>['pass1'<span>]);
                    </span><span>$connection</span> = \Yii::<span>$app</span>-><span>db;
                    </span><span>$r</span> = <span>$connection</span>->createCommand()->update('admin', ['password' => <span>$newPass</span>], 'id='.<span>$id</span>)-><span>execute();
                    </span><span>if</span>(<span>$r</span><span>){
                        Yii</span>::<span>$app</span>->user-><span>logout();
                        </span><span>return</span> <span>$this</span>-><span>goHome();
                    }</span><span>else</span><span>{
                        </span><span>return</span> <span>$this</span>-><span>goBack();
                    }
                }
            }</span><span>else</span><span>{
                Yii</span>::<span>$app</span>->session->setFlash('contact','旧密码错误'<span>);
                </span><span>return</span> <span>$this</span>->redirect(<span>array</span>('site/password'<span>));
            }
        }</span><span>else</span><span>{
            </span><span>return</span> <span>$this</span>->render('password',['model'=><span>$model</span><span>]);
        }

    }</span>
로그인 후 복사

代码优化:主要优化细节如下

  1、将控制器中的主要逻辑代码移入模型中

  2、优化数据库操作

控制器中:

<span>public</span> <span>function</span><span> actionPassword(){
        </span><span>$model</span>=<span>new</span><span> PasswordForm;
        </span><span>$request</span> = YII::<span>$app</span>-><span>request;

        </span><span>if</span>(<span>$request</span>->isPost && <span>$model</span>->load(Yii::<span>$app</span>->request->post()) && <span>$model</span>-><span>changePassword()){
            Yii</span>::<span>$app</span>->user-><span>logout();
            </span><span>return</span> <span>$this</span>-><span>goHome();
        }</span><span>else</span><span>{
            </span><span>return</span> <span>$this</span>->render('password',['model'=><span>$model</span><span>]);
        }
    }</span>
로그인 후 복사

模型中:

<span>public</span> <span>function</span><span> changePassword(){
        </span><span>$id</span> = YII::<span>$app</span>->user-><span>id;
        </span><span>$admin</span>=  Admin::findIdentity(<span>$id</span><span>);
        </span><span>$password</span> = <span>$admin</span>-><span>password;
        </span><span>if</span>(Yii::<span>$app</span>->getSecurity()->validatePassword(<span>$this</span>->password, <span>$password</span><span>)){
            </span><span>if</span>(<span>$this</span>->pass1 == <span>$this</span>-><span>pass2){
                </span><span>$newPass</span> = Yii::<span>$app</span>->getSecurity()->generatePasswordHash(<span>$this</span>-><span>pass1);
                </span><span>$admin</span>->password = <span>$newPass</span><span>;
                </span><span>if</span>(<span>$admin</span>-><span>save()){
                    </span><span>return</span> <span>true</span><span>;
                }</span><span>else</span><span>{
                    </span><span>return</span> <span>false</span><span>;
                }
            }</span><span>else</span><span>{
                Yii</span>::<span>$app</span>->session->setFlash('contact','两次新密码不相等'<span>);
                </span><span>return</span> <span>false</span><span>;
            }
        }</span><span>else</span><span>{
            Yii</span>::<span>$app</span>->session->setFlash('contact','旧密码错误'<span>);
            </span><span>return</span> <span>false</span><span>;
        }
    }</span>
로그인 후 복사

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1033242.htmlTechArticleYII框架下实现密码修改,yii框架密码修改 YII2 实现修改密码功能 主要难点: 1、密码加密 YII2对密码加密生成的结果是不同的,即用相同的...
관련 라벨:
php
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿