在codeigniter中登录身份验证

时间:2013-06-10 09:35:12

标签: codeigniter authentication login captcha

当一个人想要登录但是他/她未能进行第一次尝试然后验证码将显示并且他/她仍然只有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');
        }
    }

0 个答案:

没有答案