How can I stop PHP sleep() affecting my whole PHP code?

后端 未结 6 2095
北荒
北荒 2020-12-01 20:21

So, on my arcade, howlingdoggames.com. I have a points system that gives you a point every time you visit a page with a game on, now, to reduce abuse of this, I would like

6条回答
  •  星月不相逢
    2020-12-01 20:53

    There is (mostly) no multithreading in PHP. You can sort of do this with forking processes on Unix systems but that's irrelevant because multithreading isn't really what you're after. You just want simple logic like this:

    $now = time();
    session_start();
    $last = $_SESSION['lastvisit'];
    if (!isset($last) || $now - $last > 45) {
      $points = $_SESSION['points'];
      if (!isset($points)) {
        $points = 0;
      }
      $_SESSION['points'] = $points + 10;
      $_SESSION['lastvisit'] = $now;
    }
    

    Basically only give the points if the increment between the last time you gave points is greater than 45 seconds.

提交回复
热议问题