PHP: Detect Page Refresh

前端 未结 9 1476
余生分开走
余生分开走 2020-11-28 13:54

I have a page action.php on which I run an SQL query through the code, so that whenever the page is viewed the query runs like its like counting page vi

9条回答
  •  被撕碎了的回忆
    2020-11-28 13:57

    This can work in your scenario:

    if(isset($_GET["token"])) {
        $view_token = $_GET["token"];
    } else {
        $new_views = $views + 1;
        // UPDATE VIEWS
        $view_token = substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyz", 5)), 0, 5);
        header("Location: ?token=$view_token");
    }
    

    If the user has a token in the URL, the view count will not update. Therefore, if the user tries to refresh, it will not update the view count. When the user does not have a token in the URL, the view count updates and refreshes the page WITH a token. It is thinking outside of the box but it works!

提交回复
热议问题