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

后端 未结 6 1910
北荒
北荒 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:54

    There is no multithreading in PHP, so sleep() is always going to block your whole script.

    The way you should solve this is to record the the time of the last game, and only award points if it is more than 45 seconds later than that.

     45) {
    
        // code to award points here
    
        $_SESSION['last_game_time'] = time();
    }
    

    Bear in mind that users could still abuse this if they disable cookies (thus they will have no session data). So if that really worries you, check that they have cookies enabled before allowing them to use the feature (there are probably several questions that cover this).

提交回复
热议问题