I want to create a restriction after a action for different id's, so I decided to store the entry time and id of the post into the session variable. Then I can fetch the recent timestamp and disallow the user to do that action to id for 5 minutes to stop spamming of queries
$vars = (object) array("idpost" => $idpost,"time" => time());
$_SESSION['ids'][] = $vars;
And if the count of the array is more than 5, then stop the execution of script. But after 5 min, proceed the script.
$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;
}
}
I can't find a way to compare the most recent time stamp and check that if 5 mins have passed. How can I fetch the most recent timestamp and check that 5 minutes have passed? I can do this if i get the most recent timestamp from the array.
if (time() - $mostrecenttimestampfromarray < 5*60*60 ) // 5 mins have passed
I'm not sure what you're trying to achieve by using array_filter, is that necessary?
Can you loop through the neededObjects to find the latest timestamp and then check it. Something like this:
if ( count( $neededObject ) > 5 ) {
$latestTimestamp = 0;
foreach ( $neededObject as $object ) {
if ( $object->time > $latestTimestamp ) {
$latestTimestamp = $object->time;
}
}
if ( time() < $latestTimestamp + 5*60 ) {
return;
}
}