简体   繁体   中英

Changing the old password

I have two files which are

SiteController.php

public function actionPassword(){

        $pass = (isset($_POST['pass']) && $_POST['pass'] != "" ? $_POST['pass'] : "");
        $newPass = (isset($_POST['newpass']) && $_POST['newpass'] != '' ? $_POST['newpass'] : '');  
        $confirmPass = (isset($_POST['confirmpass']) && $_POST['confirmpass'] != '' ? $_POST['confirmpass'] : '');      
        $result = Yii::app()->params['result'];

        if($pass == '' || $newPass == '' || $confirmPass == '' ){
            $result['msg'] = "Empty password!";
        }else{
            $dbUser = new Users;
            $findUser = $dbUser->find('username = :user', array(':user' => $user));
            $verify = CPasswordHelper::verifyPassword($pass, $findUser->password);

            if(!$verify){
                $result['msg'] = "Wrong password!";
            }else{
                if($verify->status_id == 0 || $verify->status_id == '-1'){
                   $result['msg'] = "This password does not exist";

                }else if($verify->status_id == 1){
                      if ($newPass == $confirmPass){        
                         if($dbUser->save()){
                              $result['code'] = 1000;
                              $result['error'] = 1;
                              $result['msg'] = 'Password successfuly changed';
                         }else{
                              $result['code'] = 1005;
                              $result['error'] = 0;
                              $result['msg'] = $dbUser->error;
                   }
                 }          
                }  
        }   
        }

        $json = json_encode($result);
        echo $json;

}

public function actionViewPassword() {
    $this->checkLogin();
    $this->render('password');
}

and another file which is

password.php

<div class="view_container">
            <div class="pagetitle">Change Password</div>

            <div class="label_row">
                <div class="label">Old Password: </div>
                <div class="input_label"><input type="text" value="" placeholder="Old Password" id="pass" /></div>
            </div>
            <div class="label_row">
                <div class="label">New Password: </div>
                <div class="input_label"><input type="text" id="newpass" value="" placeholder="New Password" /></div>
            </div>
            <div class="label_row">
                <div class="label">Confirm New Password: </div>
                <div class="input_label"><input type="text" id="confirmpass" value="" placeholder="Confirm New Password" /></div>
            </div>
            <div class="label_row">
                <div class="label"></div>
                <div class="input_label"><input type="submit" id="btnSubmit" value="Confirm" /></div>
            </div>
        </div>

        <script>

        $(document).ready(function(){
            var userurl = '<?php echo Yii::app()->createAbsoluteUrl('user/password'); ?>';
            $('#btnSubmit').click(function(){           
                var JoldPass = $('#pass').val();
                var JnewPass = $('#newpass').val();
                var JconfirmPass = $('#confirmpass').val();
                loading(true);  
                $.ajax({
                    type: "POST",
                    url: userurl,
                    data: { 
                        pass: JoldPass,
                        newpass: JnewPass, 
                        confirmpass: JconfirmPass
                    },
                    dataType: "json",
                }).done(function(e) {
                    alert(e.msg);
                }).fail(function(e){
                    alert(e.error);
                });
            });
        });
        </script>

Okay, so here's a question, I want to get the webpage to replace the old password with a new password here. I have been trying for days but all I couldn't get the solution. I want the database to successfully change the old password. What am I missing here?

Enter old password: 11111
Enter new password: 22222
Confirm new password: 22222

Now the new password would be 22222. I would be glad if anyone could help me with this as I'm new to PHP.

I rewrited a little your code,

public function actionPassword(){

    if(empty($_POST['pass']) || empty($_POST['newpass']) || empty($_POST['confirmpass']) ){
        $result['msg'] = "Empty password!";
        $this->returnResult($result);
        return;
    }
    $pass = $_POST['pass'];
    $newPass = $_POST['newpass'];  
    $confirmPass = $_POST['confirmpass'];      
    $result = Yii::app()->params['result'];

    $dbUser = Users::model()->find('username = :user', array(':user' => $user));
    if(empty($dbUser)){
        $result['msg'] = "User not found!";
        $this->returnResult($result);
        return;
    }
    $verify = CPasswordHelper::verifyPassword($pass, $dbUser->password);

    if(!$verify){
        $result['msg'] = "Wrong password!";
        $this->returnResult($result);
        return;
    }

    //This means nothing, verifyPassword return true or false, not an object
    if($verify->status_id == 0 || $verify->status_id == '-1'){
       $result['msg'] = "This password does not exist";
       $this->returnResult($result);
       return;
    }
    if($verify->status_id == 1 && $newPass == $confirmPass){
        if($dbUser->save()){
            $result['code'] = 1000;
            $result['error'] = 1;
            $result['msg'] = 'Password successfuly changed';
            $this->returnResult($result);
            return;
        }
        $result['code'] = 1005;
        $result['error'] = 0;
        $result['msg'] = $dbUser->error;
        $this->returnResult($result);
    }  
}

protected function returnResult($result)
{
    $json = json_encode($result);
    echo $json;
}

Now there are severals problems:

  • You call $verify->status_id but $verify is the result of verifyPassword which return true or false , but not an object!
  • Nowhere in your code you assign the new Password to your object $dbUser

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM