PHP: Detect Page Refresh

前端 未结 9 1436
余生分开走
余生分开走 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 14:08

    I found this snippet here, and it worked perfectly for me:

    $pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';
    
    if($pageWasRefreshed ) {
       //do something because page was refreshed;
    } else {
       //do nothing;
    }
    
    0 讨论(0)
  • 2020-11-28 14:09

    You can save a cookie that will be associated with the present page and if any link is clicked, update the cookie with the new page name. Assuming the above is done in Javascript, you can send this updateCookie information to the server to notify about a new page hit.

    Or another approach could be, you can specify a HTTP HEADER that specifies after how much time the cache expires on a page and that way, the browser wont even send you a request about page load/refresh.

    See http://www.mnot.net/cache_docs/#IMP-SCRIPT for information about CACHING

    Also , check out Etag vs Expires in HTTP - ETag vs Header Expires

    0 讨论(0)
  • 2020-11-28 14:20
    if( $_SERVER['HTTP_CACHE_CONTROL'] == 'max-age=0')
    {
        $_SESSION['status'] = null;<br>
    }
    
    0 讨论(0)
提交回复
热议问题