在此 Codeigniter 3 应用程序中更新用户密码失败的原因是什么?

What is the reason updating a user's password fails in this Codeigniter 3 application?

我正在 Codeigniter 3.1.8 和 Bootstrap 4.

我已经为这个应用程序添加了一个注册和登录系统。我目前正在研究密码重置系统。它有两个步骤:

  1. 生成令牌并将其插入数据库(authors table).
  2. 正在设置新密码。

设置新密码失败并出现 404 错误,由于某种原因我无法找到并修复。

路线:

$route['default_controller'] = 'posts';
$route['install'] = 'install';
$route['migrate'] = 'migrate';
$route['register'] = 'register';
$route['login'] = 'login';
$route['passwordreset'] = 'passwordreset';
$route['newpasword'] = 'newpasword';
$route['newpasword/(:any)/(:any)'] = 'newpasword/index///';
$route['dashboard'] = 'dashboard';
$route['dashboard/create-post'] = 'dashboard/posts/create';
$route['dashboard/create-page'] = 'dashboard/pages/create';
$route['dashboard/create-category'] = 'dashboard/categories/create';
$route['dashboard/manage-authors'] = 'dashboard/users';
$route['404_override'] = '';
$route['categories/posts/(:any)'] = 'categories/posts/';
$route['(:any)'] = 'posts/post/';
$route['translate_uri_dashes'] = FALSE;

在新密码控制器中:

class Newpasword extends CI_Controller {

  private $hashed_email = '';
  private $token = '';

  public function index($hashed_email, $token) {
    $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->hashed_email = $hashed_email;
    $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 {
        // Encrypt new password
      $enc_password = password_hash($this->input->post('password'), PASSWORD_DEFAULT);

      // Update password column
      $this->Usermodel->set_new_password($hashed_email, $token, $enc_password);
    }
  }
}

上面controller对应的view(newpassword.php):

<?php echo form_open(base_url('newpassword')); ?>
  <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(); ?>

在模型中,我有两种插入令牌和更新密码的方法:

public function update_token($user_email, $reset_token) {
    $this->db
        ->where(['email' => $user_email])
        // insert token (make it diffrent from NULL)
        ->update('authors', array('token' => $reset_token));
}

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

插入令牌没有问题,但是设置新密码失败并出现404错误。

我做错了什么?

设置新密码后,您重定向到 /newpaswordindex 函数在 Newpasword 控制器 仅适用于 2 个参数 。您需要在控制器中创建一个新函数来查看成功页面(或使用会话警报重定向到主页)

我首先看到的是您的代码有错字问题。

Your route says form_open(base_url('newpassword')); ?> but you have defied them $route['newpasword'].

newpassword != newpasword
     ^^             ^^

在route.php

$route['newpasword'] = 'newpasword';
$route['newpasword/(:any)/(:any)'] = 'newpasword/index///';

第二个 $route['newpasword/(:any)/(:any)'] = 'newpasword/index///'; 我们不用于在 URL 中发送密码。它应该是 POST 方法,因为您使用 form_open 形式创建 method="post"。所以在代码中 $_POST['password']$this->input->post() 将起作用