我想在操作后为不同的ID创建一个限制,因此我决定将帖子的输入时间和ID存储到会话变量中。然后,我可以获取最近的时间戳,并禁止用户对该ID进行5分钟的操作,以停止发送垃圾邮件查询
$vars = (object) array("idpost" => $idpost,"time" => time());
$_SESSION['ids'][] = $vars;
如果数组的计数大于5,则停止执行脚本。但是5分钟后,继续执行脚本。
$neededObject = array_filter( //filtering the array for a specific idpost
$_SESSION['ids'],
function ($e) use ($idpost) {
return $e->idpost === $idpost;
}
);
if (count($neededObject) > 5){// user has used id it many times
$timestamps = array_filter(
$neededObject,
function ($e) use ($idpost) {
return $e->timestamp // do something here to check the recent timestamps from all others;
}
}
我找不到比较最新时间戳并检查是否经过5分钟的方法。如何获取最新的时间戳并检查是否已过去5分钟?如果我从数组中获取最新时间戳,则可以执行此操作。
if (time() - $mostrecenttimestampfromarray < 5*60*60 ) // 5 mins have passed
答案 0 :(得分:0)
我不确定您要使用array_filter尝试达到什么目的,
是否可以遍历requiredObjects以查找最新的时间戳,然后进行检查。像这样:
if ( count( $neededObject ) > 5 ) {
$latestTimestamp = 0;
foreach ( $neededObject as $object ) {
if ( $object->time > $latestTimestamp ) {
$latestTimestamp = $object->time;
}
}
if ( time() < $latestTimestamp + 5*60 ) {
return;
}
}