The DoYouHaoBaby PHP Framework中文开发文档手册
Copyright(c) DoYouHaoBaby.NET All Rights Reserved.
Copyright(c) DoYouHaoBaby.NET All Rights Reserved.
[ 修改用户密码 ]
作为一个系统的用户,有的时候,我们为了让账户更安全,隔一段时间我们需要修改一次我们的用户密码。
这里,我们也不废话了,直接开始吧。
修改密码界面,IndexController.class.php中的change_password,使用“xiaoniuge”登录,在首页访问 http://localhost/wedoit/index.php/index/change_password/id/1。
- public function change_password(){
- // 判断是否已经登录
- if( $GLOBALS['___login___']===false )
- $this->E( G::L( '你没有登录' ) );
- $this->display();
- }
访问后的效果如下:

使用旧密码和新的密码,点击修改即可。
实现修改密码的逻辑代码如下,位于IndexController.class.php中的change_the_password:
- public function change_the_password(){
- // 判断是否已经登录
- if( $GLOBALS['___login___']===false )
- $this->E( G::L( '你没有登录' ) );
- // 取得已经登录后的用户信息
- $arrUserData = $GLOBALS['___login___'];
- // 取得用户相关信息
- $sPassword = Safe::getGpc( 'user_password','P' );
- $sNewPassword = Safe::getGpc( 'new_password','P' );
- $sOldPassword = Safe::getGpc( 'old_password','P' );
- if( $sOldPassword=='' ){
- $this->E( G::L( '旧密码不能为空' ) );
- }
- if( $sPassword=='' ){
- $this->E( G::L( '新密码不能为空' ) );
- }
- if( $sPassword!=$sNewPassword ){
- $this->E( G::L( '两次输入的密码不一致' ) );
- }
- if( strlen( $sNewPassword )<6 )
- $this->E('密码不能少于 6 个字符');
- // 修改密码
- UserModel::M( )->changePassword( $arrUserData['user_name'],$sPassword,$sOldPassword );
- // 捕获行为插件中的错误
- if( UserModel::M()->isBehaviorError() ){
- $this->E( UserModel::M()->getBehaviorErrorMessage());
- }
- else{
- // 取得用户模型
- $oUser = UserModel::F( 'user_id =?',$arrUserData['user_id'] )->query();
- // 捕获错误
- if( $oUser->isError() ){
- $this->E( $oUser->getErrorMessage( ) );
- }
- $this->setCookieData( 'user_login',null,CURRENT_TIMESTAMP-8640000 );
- $this->assign( '__JumpUrl__',G::U( 'user/login' ) );
- $this->S( G::L( '密码修改成功,你需要重新登录!' ) );
- }
- }
修改密码成功后,你可以使用的新的密码登录网站了。
