1

当一个人想要登录但他/她第一次尝试失败时,验证码将显示并且他/她只尝试了 3 次但他/她失败了所有的扩孔尝试,然后在特定的 IP 登录将被阻止一段时间如何进入codeigniter

这是我的代码

function login() {
    $this->form_validation->set_rules('username', 'Username', 'trim|required');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|md5');
    //$this->session->unset_userdata('count');
    if($this->session->userdata('count')) {

        $this->form_validation->set_rules('captcha', 'Captcha', 'required|callback_recaptcha_validation');
        $this->session->unset_userdata('count');
    }

    if ($this->form_validation->run() == TRUE) {
        $checkAdminLoginDeatils = $this->admin_model->adminLogin();
        if ($checkAdminLoginDeatils == false) {
            $val='0';
            $val=$this->session->set_userdata('validate','0');
            $val=$val+1;
            $ip_address = $this->input->ip_address();
            $record = $this->db->where('IP', $ip_address)->get('LoginAttempts')->row();
            $this->session->set_userdata('count', '1');
            $vals = array(
                    'img_path'   => './captcha/',
                    'img_url'    =>  base_url().'/captcha/'
            );

            $cap = create_captcha($vals);

            $captcha = array(
                    'captcha_time'  => $cap['time'],
                    'ip_address'    => $this->input->ip_address(),
                    'word'   => $cap['word']
            );

            $data['cap']=$cap;
            $this->session->set_userdata('message', 'Invalid username or password');
            $this->db->select('*');
            $this->db->where('IP',$this->input->ip_address());
            $query=$this->db->get('LoginAttempts');
           if($query->num_rows() == 1) {
                $this->db->set('LastLogin',time());
                if(isset ($val))
                    $this->db->set('Attempts',$val);
                $this->db->where('IP', $this->input->ip_address());
                $result=$this->db->update('LoginAttempts');
            }
            else {
                $insert = array(
                        'LastLogin' => time(),
                        'Attempts' => $val,
                        'IP'    => $this->input->ip_address()
                );


                $query = $this->db->insert_string('LoginAttempts', $insert);
                $this->db->query($query);
            }
            //$time=now();


            $expiration = time()-7200; // Two hour limit
            $this->db->query("DELETE FROM LoginAttempts WHERE LastLogin < ".$expiration);
        } else {
            redirect('admin/admin/dashboard');
        }
    }
4

0 回答 0