douti9286 2019-01-09 19:31
浏览 81
已采纳

使用password_verify登录CodeIgniter

I'm trying to achieve to login implementation in CodeIgniter, I'm hashing password while registration like password_hash($this->input->post('password'),PASSWORD_DEFAULT) in my Controller and in the same Controller I'm trying to write a login method which is as followed :

public function loginValidation() {
        $this->form_validation->set_rules('email', 'Email', 'trim|required');
        $this->form_validation->set_rules('password', 'Password', 'trim|required');

        if ($this->form_validation->run()) {
            // true
            $email  =   $this->input->post('email');
            $password   =   $this->input->post('password');

            // User Model Loaded in constructor
            if ($this->user->canLogin($email, $password)) {
                $session_data   =   array('email' => $email );
                $this->session->set_userdata($session_data);
                redirect('profile/personal','Refresh');

            } else {
                $this->session->set_flashdata('error', 'Invalid Username or Password');
                //redirect('login','Refresh');

            }
        } else {
            // try again to login
            //redirect('login','Refresh');
        }
    }

My user Model function is

public function canLogin($email, $password) {
    $this->db->where('email',$email);
    $this->db->where('password',$password);
    $query  =   $this->db->get($this->tableName);

    if ($query->num_rows() > 0) {
        return true;
    } else {
        return false;
    }
}

I know I have to password_verify($string,$hash) at some point but I'm unable to figure out.

How do I validate the password against email and redirect to the desired view i.e. personal/profile and I'm making request via AJAX call.

  • 写回答

1条回答 默认 最新

  • duannong1801 2019-01-09 19:44
    关注

    What you need to do is fetch the record from the DB where only the email matches (assuming it is the Unique key). Then you compare the returned value using password_verify().
    This is very rough and untested, but should give you an idea:

    public function canLogin($email, $password) {
        $this->db->where('email',$email);
        // $this->db->where('password',$password);
        $query  =   $this->db->get($this->tableName);
    
        $row = $query->row();
        return $row ? password_verify($password, $row->password) : false;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀