0

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
weegee
  • 3,256
  • 2
  • 18
  • 32
  • Have you read about sleep() function in php? – Rahul Apr 05 '19 at 06:22
  • @RahulMeshram it will make the whole page to sleep, then different id's cannot use that from the same session. That's why i stored the array with objects of id and time – weegee Apr 05 '19 at 06:24
  • @RahulMeshram Actually [sleep()](https://stackoverflow.com/questions/3930736/what-is-a-practical-use-for-phps-sleep) creates a delay between scripts, i dont want to add a delay, i want to stop the execution and wait for 5 mins for different id's – weegee Apr 05 '19 at 06:36

1 Answers1

0

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;
    }   
}
Matt
  • 93
  • 1
  • 6
  • Actually i'm using array_filter to filter out timestamps for a particular id. So user can use other id's too – weegee Apr 05 '19 at 07:05
  • Why $latestTimestamp is 0? – weegee Apr 05 '19 at 07:07
  • @window.document I can see that that the first array_filter is filtering ids, but I don't see what the second one does, inside the if statement? $latestTimestamp = 0, is to prevent undefined variable notice. – Matt Apr 05 '19 at 07:15
  • The second one is to filter timestamps to the most recent one which i don't know just for understanding i put it there. It's unnecessary then but here `$object->time > $latestTimestamp ` you are checking if the timestamp exists because $latestTimestamp is 0. I want to fetch the most recent timestamps. There are many objects in the array – weegee Apr 05 '19 at 07:17
  • @window.document the foreach is looping your `$nestedObject`. `$latestTimestamp` starts at 0 and is updated with the first timestamp and then updated with every further iteration if the timestamp is greater than the current one store in `$latestTimestamp`. When the loop finishes, you end up with the latest timestamp in `$latestTimestamp`. – Matt Apr 05 '19 at 07:22
  • It doesn't work, i set the value for 1 minute, and left my computer for 3 minutes, it doesn't works. Maybe use my if statement in the question? – weegee Apr 05 '19 at 07:40
  • I took my observations from [here](https://stackoverflow.com/questions/14035845/how-to-blocked-login-a-few-minutes-after-3-unsuccessful-login) see the accepted answer – weegee Apr 05 '19 at 08:08
  • @window.document it's actually `5*60` not `5*60*60`. I have updated my answer does this now work for you? – Matt Apr 05 '19 at 08:22
  • it works. I like to apologize for my error in the question which lead you to the same error. Thank you very much :) – weegee Apr 05 '19 at 15:38