簡體   English   中英

為什么在此 Codeigniter 3 應用程序中嘗試更新用戶密碼會失敗?

[英]Why do attempts to update a user's password in this Codeigniter 3 application fail?

我正在Codeigniter 3.1.8和 Bootstrap 4 中開發一個基本的博客應用程序

我在這個應用程序中添加了一個注冊和登錄系統。 我目前正在研究密碼重置系統。

我無法弄清楚為什么更新用戶密碼失敗如下:

在此處輸入圖像描述

在新密碼 controller我有:

class Newpassword extends CI_Controller {
    private $token = NULL;

    public function index($token = NULL) {
        //echo $token; die();
        $data = $this->Static_model->get_static_data();
        $data['pages'] = $this->Pages_model->get_pages();
        $data['tagline'] = 'New password';
        $data['categories'] = $this->Categories_model->get_categories();
        $this->token = $token;
    
         // Form validation rules
         $this->form_validation->set_rules('password', 'Password', 'required|min_length[6]');
         $this->form_validation->set_rules('cpassword', 'Confirm password', 'required|matches[password]');
         $this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');
    
        if(!$this->form_validation->run()) {
            $this->load->view('partials/header', $data);
            $this->load->view('auth/newpassword');
            $this->load->view('partials/footer');
        } else {
            $this->add();
        }
    }

    public function add() {
        $data = $this->Static_model->get_static_data();
        $data['pages'] = $this->Pages_model->get_pages();
        $data['tagline'] = 'New password';
        $data['categories'] = $this->Categories_model->get_categories();
    
        // Encrypt new password
        $enc_password = password_hash($this->input->post('password'), PASSWORD_DEFAULT);
    
        // Update password column
        $token = $this->token;
    
        if ($this->Usermodel->set_new_password($token, $enc_password)) {
            redirect('login'); 
            $this->session->set_flashdata("new_password_success", "Your new password was set. You can login");
        } else {
            $this->session->set_flashdata("new_password_fail", "We have failed updating your password");
            redirect('/newpassword'); 
        }
      }
    }

在用戶模型 model我有:

public function set_new_password($token, $enc_password) {
    $this->db
        ->where(['token' => $token])
        // set new password and reset token to NULL
        ->update('authors', array('password' => $enc_password, 'token' => NULL));
}

表格:

<?php echo form_open(base_url('newpassword/add')); ?>
  <div class="form-group <?php if(form_error('password')) echo 'has-error';?>">
    <input type="password" name="password" id="password" class="form-control" placeholder="Password">
    <?php if(form_error('password')) echo form_error('password'); ?> 
  </div>
  <div class="form-group <?php if(form_error('cpassword')) echo 'has-error';?>">
    <input type="password" name="cpassword" id="cpassword" class="form-control" placeholder="Confirm password">
    <?php if(form_error('cpassword')) echo form_error('cpassword'); ?> 
  </div>
  <div class="form-group mb-2">
    <input type="submit" value="Set password" class="btn btn-block btn-md btn-success">
  </div>            
<?php echo form_close(); ?>

如果我取消注釋//echo $token; die(); //echo $token; die(); 從 controller 開始,令牌以預期值打印。 然而,密碼不會更新(並且更新失敗會正確顯示)。

我究竟做錯了什么?

當您單擊“設置密碼”按鈕重定向到新密碼newpassword/add但此處不存在令牌時,因為僅在上一步使用它:

  1. 密碼重置請求
  2. 點擊 email 中的鏈接(令牌存在)
  3. 填寫表格並單擊“設置密碼”(將表格數據發送到newpassword/add ) - 此處不存在令牌但您想使用它: $token = $this->token; 但現在“令牌”是:添加

抱歉,您必須重構密碼重置代碼邏輯。 我認為這比重寫整個過程更難解決

更新:

添加$data['token'] = $token; 進入Newpassword controller中的索引方法

views/auth/newpassword.php中的表單操作url修改為: <?php echo form_open(base_url('newpassword/add/'.$token)); ?> <?php echo form_open(base_url('newpassword/add/'.$token)); ?>

重要的!!!

在 model 中添加返回到 ell 函數: return $this->db->....並在設置 session flashdata之后放置redirect('<url>')

我測試了它,我認為解決了這個問題。

這是密碼更新的最終工作代碼(以防其他人需要):

在新密碼 controller中:

class Newpassword extends CI_Controller
{
    public function index($token = NULL)
    {
        $data               = $this->Static_model->get_static_data();
        $data['pages']      = $this->Pages_model->get_pages();
        $data['tagline']    = 'New password';
        $data['categories'] = $this->Categories_model->get_categories();
        $data['token']      = $token;
        
        // Form validation rules
        $this->form_validation->set_rules('password', 'Password', 'required|min_length[6]');
        $this->form_validation->set_rules('cpassword', 'Confirm password', 'required|matches[password]');
        $this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');
        
        if (!$this->form_validation->run()) {
            $this->load->view('partials/header', $data);
            $this->load->view('auth/newpassword');
            $this->load->view('partials/footer');
        } else {
            // Encrypt new password
            $enc_password = password_hash($this->input->post('password'), PASSWORD_DEFAULT);
            
            if ($this->Usermodel->set_new_password($token, $enc_password)) {
                $this->session->set_flashdata("new_password_success", "Your new password was set. You can login");
                redirect('login');
            } else {
                $this->session->set_flashdata("new_password_fail", "We have failed updating your password");
                redirect('/newpassword/' . $token);
            }
        }
    }
}

在 model 中:

public function set_new_password($token, $enc_password) {
    return $this->db
        ->where('token', $token)
        // set new password and reset token to NULL
        ->update('authors', array('password' => $enc_password, 'token' => NULL));
}

表格(視圖):

<?php echo form_open(base_url('newpassword/'. $token)); ?>
  <div class="form-group <?php if(form_error('password')) echo 'has-error';?>">
    <input type="password" name="password" id="password" class="form-control" placeholder="Password">
    <?php if(form_error('password')) echo form_error('password'); ?> 
  </div>
  <div class="form-group <?php if(form_error('cpassword')) echo 'has-error';?>">
    <input type="password" name="cpassword" id="cpassword" class="form-control" placeholder="Confirm password">
    <?php if(form_error('cpassword')) echo form_error('cpassword'); ?> 
  </div>
  <div class="form-group mb-2">
    <input type="submit" value="Set password" class="btn btn-block btn-md btn-success">
  </div>            
<?php echo form_close(); ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM