Advertisement
Guest User

Untitled

a guest
May 8th, 2012
1,617
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.95 KB | None | 0 0
  1.     public function isBlocked()
  2.     {
  3.         // Get this user IP Address.
  4.         //
  5.         $ip_address = $this->CI->input->ip_address();
  6.  
  7.         // Time that a user gets blocked.
  8.         //
  9.         $blockTime = 1800;
  10.  
  11.         // Check if we have the user record.
  12.         //
  13.         $record = $this->CI->db->where('ip_address', $ip_address)->get('login_attempts')->row();
  14.         if ( ! empty( $record ) ):
  15.             // Check this user login attempts.
  16.             //
  17.             if ( $record->attempts >= 3 ):
  18.                 // Check if the user block has expired.
  19.                 //
  20.                 if( ( time() - $record->lastLogin ) > $blckTime ):
  21.                     // User is not blocked anymore.
  22.                     //
  23.                     return false;
  24.                 else:
  25.                     // The user is blocked.
  26.                     //
  27.                     return true;
  28.                 endif;
  29.             endif;
  30.         endif;
  31.  
  32.         // The user is not blocked.
  33.         //
  34.         return false;
  35.     }
  36.  
  37.  
  38.     private function loginAttempt( $passed = false )
  39.     {
  40.         // Get this user IP Address.
  41.         //
  42.         $ip_address = $this->CI->input->ip_address();
  43.  
  44.         // If the user logged in with success.
  45.         //
  46.         if ( $passed ):
  47.             // Clear this user loginAttempts.
  48.             //
  49.             $this->CI->db->where('ip_address', $ip_address)->update('login_attempts', array( 'attempts' => 0, 'lastLogin' => time() ) );
  50.  
  51.         // This is a failed login attempt.
  52.         //
  53.         else:
  54.             // Check if we have the user record.
  55.             //
  56.             $record = $this->CI->db->where('ip_address', $ip_address)->get('login_attempts')->row();
  57.             if ( empty( $record ) ):
  58.                 // Create the user record.
  59.                 //
  60.                 $this->CI->db->insert('login_attempts', array( 'ip_address' => $ip_address, 'attempts' => 1, 'lastLogin' => time() ) );
  61.  
  62.             // We do, check if the user needs to be blocked.
  63.             //
  64.             else:
  65.                 // The user exceeded the login attempts.
  66.                 //
  67.                 if ( $record->attempts < 3 ):
  68.                     // Update the user record.
  69.                     //
  70.                     $this->CI->db->where('ip_address', $ip_address)->update('login_attempts', array( 'attempts' => ( $record->attempts + 1), 'lastLogin' => time() ) );
  71.                 endif;
  72.             endif;
  73.         endif;
  74.  
  75.         // We are done here.
  76.         //
  77.         return true;
  78.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement