Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function isBlocked()
- {
- // Get this user IP Address.
- //
- $ip_address = $this->CI->input->ip_address();
- // Time that a user gets blocked.
- //
- $blockTime = 1800;
- // Check if we have the user record.
- //
- $record = $this->CI->db->where('ip_address', $ip_address)->get('login_attempts')->row();
- if ( ! empty( $record ) ):
- // Check this user login attempts.
- //
- if ( $record->attempts >= 3 ):
- // Check if the user block has expired.
- //
- if( ( time() - $record->lastLogin ) > $blckTime ):
- // User is not blocked anymore.
- //
- return false;
- else:
- // The user is blocked.
- //
- return true;
- endif;
- endif;
- endif;
- // The user is not blocked.
- //
- return false;
- }
- private function loginAttempt( $passed = false )
- {
- // Get this user IP Address.
- //
- $ip_address = $this->CI->input->ip_address();
- // If the user logged in with success.
- //
- if ( $passed ):
- // Clear this user loginAttempts.
- //
- $this->CI->db->where('ip_address', $ip_address)->update('login_attempts', array( 'attempts' => 0, 'lastLogin' => time() ) );
- // This is a failed login attempt.
- //
- else:
- // Check if we have the user record.
- //
- $record = $this->CI->db->where('ip_address', $ip_address)->get('login_attempts')->row();
- if ( empty( $record ) ):
- // Create the user record.
- //
- $this->CI->db->insert('login_attempts', array( 'ip_address' => $ip_address, 'attempts' => 1, 'lastLogin' => time() ) );
- // We do, check if the user needs to be blocked.
- //
- else:
- // The user exceeded the login attempts.
- //
- if ( $record->attempts < 3 ):
- // Update the user record.
- //
- $this->CI->db->where('ip_address', $ip_address)->update('login_attempts', array( 'attempts' => ( $record->attempts + 1), 'lastLogin' => time() ) );
- endif;
- endif;
- endif;
- // We are done here.
- //
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement