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 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程