我想要做的是弄清楚我应该在哪里放置一段代码,它需要检查是否尝试了最大登录尝试,然后它会检查 10 分钟是否已经过去,但这样用户可以尝试再次登录。不知道应该如何添加这个逻辑。
function submit()
{
// Sets validation rules for the login form
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
$this->form_validation->set_rules('remember', 'Remember me', 'integer');
// Checks to see if login form was submitted properly
if ($this->form_validation->run() === false)
{
$outputArray = array('error' => 'yes', 'message' => 'There was a problem submitting the form! Please refresh the window and try again!');
}
else
{
if (is_null($userData = $this->usersmodel->getUserByUsername($this->input->post('username'))))
{
// Username was not found in the database
$outputArray = array('error' => 'yes', 'message' => 'Incorrect username and password combination!');
}
else
{
// Checks to see if user has exceeded max login attempts
if ($this->auth->isMaxLoginAttemptsExceeded($userData->userID))
{
// Max was exceeded and sends email to account holder
$outputArray = array('error' => 'yes', 'message' => 'Your account is currently locked, we appologize for the inconvienence. You must wait 10 minutes before you can login again! An email was sent to the owner of this account!');
$userData = array('userID' => $userData->userID, 'firstName' => $userData->firstName, 'lastName' => $userData->lastName, 'email' => $userData->email, 'username' => $userData->username);
$this->auth->sendEmail($this->config->item('defaultTemplate'), 'maxlogins', 'KOW Manager Account Locked', $userData);
}
else
{
// Matches user's status for validity
switch($userData->usersStatusesID)
{
// Registered not validated
case 1:
$outputArray = array('error' => 'yes', 'message' => 'Sorry you must verify your account before logging in!');
break;
// Account suspended
case 3:
$outputArray = array('error' => 'yes', 'message' => 'Your account has been suspended!');
break;
// Account Banned
case 4:
$outputArray = array('error' => 'yes', 'message' => 'Your account is currently banned!');
break;
// Account Deleted
case 5:
$outputArray = array('error' => 'yes', 'message' => 'Your account has been deleted!');
break;
// Registered and validated
default:
// Checks to see if login was successful
if ($this->auth->login($this->input->post('username'), $this->input->post('password'), $this->input->post('remember')))
{
// Login was successful
$outputArray = array('success' => 'Yes', 'message' => 'Sending to control panel!');
}
else
{
// Login failed
$outputArray = array('error' => 'yes', 'message' => 'Incorrect username and password combination!');
}
}
}
}
}
echo json_encode($outputArray);
}
/**
* Check if login attempts exceeded max login attempts
*
* @param integer
* @return bool
*/
function isMaxLoginAttemptsExceeded($userID)
{
$this->ci->load->model('users/usersmodel');
$loginAttempts = $this->ci->usersmodel->getLoginAttemptsNum($this->ci->input->ip_address(), $userID);
if ($loginAttempts >= 5)
{
return true;
}
else
{
return false;
}
}
/**
* Get number of attempts to login occured from given IP-address or username
*
* @param string
* @param string
* @return integer
*/
function getLoginAttemptsNum($ipAddress, $userID)
{
$this->db->where('ipAddress', $ipAddress);
$this->db->or_where('userID', $userID);
$query = $this->db->get($this->usersLoginsAttempts);
if ($query->num_rows > 0)
{
return $query->num_rows;
}
else
{
return 0;
}
}
Fields: id, userID, ipAddress, datetime
每次用户进行不正确的登录时,它都会在字段中添加另一行,该字段按 ipAddress 或 userID 中的每 5 个来存储它。所以它需要查看最后一个日期时间,因为它只存储最后 5 个。