php - Codeignter Limit Login to Four Times a Day -


hello guys want limit login four(4) per day.

i'm working this.

do need set login attempts userdata?

anyone there done already?

controller

// database work     $data = $this->login_model->validate_login($this->input->post('username'), $this->input->post('password')); 

model

public function validate_login($username, $password) {      $this->load->helper('password');      $this->db->select('user_id, username, password, date_registered, active, login_attempts');     $this->db->from('users');     $this->db->where('username', $username);     $this->db->limit(1);      $query = $this->db->get();      if($query->num_rows() == 1) {        $row = $query->row();         if (hash_password($password) == $row->password) {            $array['user_id'] = $row->user_id;            $array['username'] = $row->username;            $array['date_registered'] = $row->date_registered;            $array['active'] = $row->active;            return $array;        }     }      // login attempts +1 because login failed     $this->_increase_login_attempts($username);     return (1 + (isset($row) ? $row->login_attempts : 0));  }  private function _increase_login_attempts($username) {         $this->db->set('login_attempts', 'login_attempts + 1', false);         $this->db->where('username', $username);         $this->db->update('users');          if ($this->db->affected_rows() == 1) {             return true;         }         return false;     } 

you should store attempts count (or records if like) in database (e.g. mysql, or redis), not in session data.
because session can clear if user cleared browser cookies.


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -