php - Codeigniter - 将用户输入的密码与存储在数据库中的散列密码进行匹配始终返回 false。请帮忙

标签 php mysql oop model-view-controller codeigniter

我有我的 Controller 设置 我的加入模型 和我的login_model

用户注册,在通过所有验证和验证码等并单击提交后,他们将被设置为设置。

我的设置 Controller 加载 join_model 和 create() 方法,获取所有发布数据并准备将其发送到数据库。这是当他们在注册表单中输入的密码被散列、加盐等处理时。

在此之后,我有一个 if 语句,用于检查用户是否通过了 login_model 中的 checkLogin 方法(值为 TRUE)。该方法位于设置 Controller 中,我将其称为loginValidated。

如果这是 TRUE,则用户将被重定向到成员(member)区域(破折号)。

当我测试时,我不断被发送到失败的页面。我还将 if 状态更改为 (!this->loginValidated()),然后我被重定向到帐户区域,这意味着密码必须不匹配。

我想知道是否有人可以快速浏览一下我的代码,看看他们是否发现我出错的地方?

<?php


class Setup extends Controller {

    public function index() {

        $this->load->model('join_model');
        $this->join_model->create();
        if ($this->loginValidated()) { //if user credentials passed validation

        redirect('dash'); //forward to dashboard
        }
        else {
            redirect('failed');
        }
    }
    public function loginValidated() {
        $this->load->model('login_model'); //load login_model model
        $this->login_model->checkLogin(); //load checkLogin method

    }
 }



<?php
//MY CONTROLLER

class Login_Model extends CI_Model {

    public function checkLogin() {
            return Join_Model::$u;

            $this->db->where('email', $this->input->post('email')); //compare db email to email entered in form
            $this->db->where('password', $u->password); //compare db password to password entered by user after hashing
            $query = $this->db->get('user'); // get the above info from 'user' table

            if ($query->num_rows == 1) { //if number of rows returned is 1

            $this->load->library('session');
            $this->session->set_userdata('user_id',$u->id);
            $this->session->set_userdata('username',$u->username);
            $this->session->set_userdata('first_name',$u->first_name);
            $this->session->set_userdata('last_name',$u->last_name);
            $this->session->set_userdata('logged_in', 'TRUE');

            return TRUE;
      }
   }
}


<?php
// MY JOIN MODEL

class Join_Model extends CI_Model {
    public static $u;
    public function create() {
                $this->load->helper('date');
                $this->load->library('encrypt');

  $u->first_name = $this->input->post('first_name');
                $u->last_name = $this->input->post('last_name');
                $u->email = $this->input->post('email');

                // sha1 and salt password
                $salt = $this->config->item('encryption_key');
  $password = $this->encrypt->sha1($this->input->post('password'));
                $start_hash = sha1($salt . $password);
                $end_hash = sha1($password . $salt);
                $hashed = sha1($start_hash . $password . $end_hash);
                $u->password = sha1($hashed);

                $u->birthday = $this->input->post('year') . '-' . $this->input->post('month') . '-' . $this->input->post('day');
                $u->sex = $this->input->post('sex');

                $u->created_at = date('Y-m-d H:i:s', now()); // date and time user joined the website

                $this->db->insert('user', $u);

    }
}

最佳答案

我的 CI 有点生疏,但看起来您在 checkLogin() 函数的第一行返回了一些内容,在我看来,下面的代码没有被执行那么。

此外,在将密码发送到数据库之前,您需要再次对密码进行哈希处理,否则您将把明文密码与哈希值进行比较。

此外,在我看来,您需要一个用户 Controller 和模型,而不是用户注册和登录的模型。此外,您应该避免在模型中加载库,这在使用 MVC 框架时被认为是丑陋的。

关于php - Codeigniter - 将用户输入的密码与存储在数据库中的散列密码进行匹配始终返回 false。请帮忙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4497677/

相关文章:

javascript - javascript 中的新输入在单击时出现并消失

php - 如何从 Webhook 获取数据以存储在数据库中?

MySQL:如果另一个表中的值为 NULL,如何将值插入表中?

mysql - SQL 选择在另一列中至少具有一个特定值的所有非唯一行

javascript - 使用javascript,一个对象如何从另一个对象派生,而基础对象可以独立调用?

如果存在,PHP重命名文件名将数字附加到结尾

php - 带有 PHP 确认邮件的预订系统

MySQL 单查询基准测试策略

java - 如何从构造函数中获取值到方法中?

php - 扩展 mysqli_stmt 以使用自定义 mysqli_result