我想要做的是弄清楚我应该在哪里放置一段需要检查的代码以查看是否尝试了最大登录尝试然后它会检查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中的每一个存储它。所以它需要查看最后一个日期时间,因为它只存储了最后一个日期。
答案 0 :(得分:1)
您的首选是在以下情况下是否锁定用户:
您设置的方法是这些方法的第二步(根据您的函数isMaxLoginAttemptsExceeded($ userData-> userID)判断))
所以,基于此:
第1阶段:
向用户的数据库添加3个字段:“invalid_login_count”,“last_invalid”尝试“和”locked_out_until“
第1阶段:
在“//登录失败”行周围,您需要记录无效尝试的事实。增加“invalid_login_count”并存储在DB中,将“last_invalid_attempt”设置为now。如果“invalid_login_count”= 5,则还将“locked_out_until”更新为时间+5分钟。
第二阶段:
围绕“//登录成功”行清除“invalid_login_count”,“last_invalid_attempt”和“locked_out_until”(即重置)的值
第3阶段:
如果“last_invalid_attempt”有一个值,并且它小于5分钟前,则清除“locked_out_until”,“last_invalid_attempt”和“invalid_login_count”中的值。只要您获得用户详细信息,无论是无效还是现在,请立即执行此操作。
第四阶段:
您的$ this-> auth-> isMaxLoginAttemptsExceeded($ userData-> userID)函数应该查看“locked_out_until”值,如果>现在,他们被锁定了。
注意:这意味着用户无法输入5次无效尝试<每一个之间有5分钟的差距。这不是你要求的完美(5分钟内5次尝试),但存储所有最新尝试的时间和仅包括最后5分钟的时间的逻辑有点难度 - 所以我保持简单。
请注意,我实际上建议通过IP地址锁定,这是一个略有不同的逻辑,因为你需要有一个特定的表来计算尝试和通过IP锁定,你还需要在两个地方更新(如果用户名找不到,或者如果密码无效),如果IP被锁定,你应该把用户扔掉,甚至在检查用户名是否有效之前。否则,它是相同的逻辑。