The DoYouHaoBaby PHP Framework中文开发文档手册
Copyright(c) DoYouHaoBaby.NET All Rights Reserved.
[ 修改用户密码 ]

作为一个系统的用户,有的时候,我们为了让账户更安全,隔一段时间我们需要修改一次我们的用户密码。

这里,我们也不废话了,直接开始吧。

修改密码界面,IndexController.class.php中的change_password,使用“xiaoniuge”登录,在首页访问 http://localhost/wedoit/index.php/index/change_password/id/1。

  1. public function change_password(){   
  2.            
  3.         // 判断是否已经登录   
  4.         if(  $GLOBALS['___login___']===false  )   
  5.             $this->E( G::L( '你没有登录' ) );   
  6.   
  7.         $this->display();   
  8.     }  


访问后的效果如下:


使用旧密码和新的密码,点击修改即可。

实现修改密码的逻辑代码如下,位于IndexController.class.php中的change_the_password:
  1. public function change_the_password(){   
  2.   
  3.         // 判断是否已经登录   
  4.         if(  $GLOBALS['___login___']===false  )   
  5.             $this->E( G::L( '你没有登录' ) );   
  6.   
  7.         // 取得已经登录后的用户信息   
  8.         $arrUserData = $GLOBALS['___login___'];   
  9.   
  10.         // 取得用户相关信息   
  11.         $sPassword = Safe::getGpc( 'user_password','P' );   
  12.         $sNewPassword = Safe::getGpc( 'new_password','P' );   
  13.         $sOldPassword = Safe::getGpc( 'old_password','P' );   
  14.   
  15.         if( $sOldPassword=='' ){   
  16.   
  17.            $this->E( G::L( '旧密码不能为空' ) );   
  18.         }   
  19.   
  20.         if( $sPassword=='' ){   
  21.   
  22.             $this->E( G::L( '新密码不能为空' ) );   
  23.         }   
  24.   
  25.         if( $sPassword!=$sNewPassword ){   
  26.   
  27.             $this->E( G::L( '两次输入的密码不一致' ) );   
  28.         }   
  29.   
  30.         if( strlen( $sNewPassword )<6 )   
  31.             $this->E('密码不能少于 6 个字符');   
  32.   
  33.         // 修改密码   
  34.         UserModel::M( )->changePassword(  $arrUserData['user_name'],$sPassword,$sOldPassword );   
  35.   
  36.         // 捕获行为插件中的错误   
  37.         if( UserModel::M()->isBehaviorError() ){   
  38.   
  39.             $this->E( UserModel::M()->getBehaviorErrorMessage());   
  40.         }   
  41.   
  42.         else{   
  43.   
  44.            // 取得用户模型   
  45.            $oUser = UserModel::F( 'user_id =?',$arrUserData['user_id'] )->query();   
  46.   
  47.            // 捕获错误   
  48.           if( $oUser->isError() ){   
  49.   
  50.               $this->E( $oUser->getErrorMessage( ) );   
  51.            }   
  52.   
  53.            $this->setCookieData( 'user_login',null,CURRENT_TIMESTAMP-8640000  );   
  54.            $this->assign( '__JumpUrl__',G::U( 'user/login' ) );   
  55.            $this->S( G::L( '密码修改成功,你需要重新登录!' ) );   
  56.        }   
  57.    }  


修改密码成功后,你可以使用的新的密码登录网站了。