我想锻炼一个功能,以便用户的不成功登录尝试上限为5,然后为他们提供相应的消息。
以下是我的代码:
$attempts = $this->input->post('attempts');
if ($attempts <= '5')
{
$this->db->where('username', $username);
$this->db->set('attempts', '`attempts`+ 1', FALSE);
$this->db->update('tb_user');
}
else
{
echo "Your account is locked!";
}
以上代码可以更新数据库表和列尝试,但在5次尝试后记录仍在更新,如:存储第6次,第7次和第n次登录尝试失败。
我做错了什么,如何解决?
答案 0 :(得分:1)
从数据库中获取尝试次数,而不是从前端传递。
$attempts = $this->db->where('username', $username)->get('tb_user')->row()->attempts;
if ($attempts > 4)
{
echo "Your account is locked!";
}
else
{
$this->db->where('username', $username);
$this->db->set('attempts', ($attempts + 1), FALSE);
$this->db->update('tb_user');
}