php - 下一步我该怎么做才能阻止登录表单对数据库的 MySQL 查询尝试?
问题描述
我已经设法实现了连接到 IP 数据库的代码,并使用 users 表中的相应 user_id 以及 IP 地址和时间戳注册失败的尝试。
然后我查询数据库,并以 10 分钟的间隔检查最后一次登录尝试。
用户可以尝试另一次登录尝试,并将其记录到数据库中。
我不确定我现在必须做什么来限制在 3 次失败后再次查询的数据库?
我应该查什么?
代码是:
function timestampCount($ip,$id,$mysqli): int
{
mysqli_query($mysqli, "INSERT INTO `ip` (`id`, `address` ,`timestamp`)VALUES ('$id','$ip',CURRENT_TIMESTAMP)");
$result = mysqli_query($mysqli, "SELECT COUNT(*) FROM `ip` WHERE `address` LIKE '$ip' AND `timestamp` > (now() - interval 10 minute)");
$count = mysqli_fetch_array($result, MYSQLI_NUM);
if($count[0] > 3){
echo "Your are allowed 3 attempts in 10 minutes";
return 1;
}
else {
return 0;
}
}
然后我用
$attempts = (new ip_request)->timestampCount($ip, $id,$mysqli);
在我的登录页面上。
正如我认为我可能会得到 1 或 0,然后根据哪个,设置$hideLogin = true;
,并使用类似的东西。
但是每次用户刷新页面时,变量都会被重置。
因此,我不确定接下来如何处理登录页面以阻止再次查询数据库以进行后续登录尝试。
解决方案
我设法得到了一个可行的解决方案。
我把它timestampCount()
分成一个getter和setter。
然后我打电话getTimestampCount()
来检查登录页面,如果用户在数据库中有 3 个或更多条目。
如果他们这样做了,它会显示一条消息,并停止执行对数据库的密码调用的其余代码。
如果数据库中的条目不超过 3 个,则错误的尝试被跟踪并设置setTimestampCount()
function getTimestamp($email,$mysqli): bool
{
$result = $mysqli->prepare ("SELECT COUNT(*) FROM `ip` INNER JOIN users ON users.id = ip.id WHERE `email` LIKE ? AND `timestamp` > (now() - interval 10 minute)");
$result ->bind_param("s", $email);
$result->execute();
$final = $result->get_result();
$count = mysqli_fetch_array($final, MYSQLI_NUM);
if($count[0] > 2){
return true;
}
else {
return false;
}
}
function setTimestampCount($ip,$id,$mysqli)
{
$query = $mysqli->prepare ("INSERT INTO `ip` (`id`, `address` ,`timestamp`)VALUES (?,?,CURRENT_TIMESTAMP)");
$query ->bind_param("is", $id, $ip);
$query->execute();
}
一切都经过测试和工作。
推荐阅读
- c# - 超过执行时间限制 c#,同时在递归中使用 Math.Pow()
- java - 在 Spring Boot 中创建用户时,角色存储库 findAll 方法返回 null
- sql - 是否可以强制 Oracle 在使用用户定义的视图和函数的查询中应用视图合并?
- javascript - React-Select - 替换自定义选项内容的组件
- apache - Apache 多个 VirtualHost 具有相同的服务器名称、相同的 IP 和端口但不同的 DocumentRoot,可能吗?
- javascript - 从层次结构javascript创建数组
- c - 尝试接收字符并删除空格
- peoplesoft - 应用程序引擎程序 - 发生 NULL 插入错误
- jquery - 仅在选中复选框时克隆单击的 div
- c# - 如何将 C++ 回调函数传递给 C# COM 方法