By Moon

2010-11-27 05:09:49 8 Comments

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 views

mysqli_query("UPDATE ****");

The problem is when the page is refreshed, the query is run & PAGE REFRESH is counted as a PAGE VIEW which I want to avoid.

   Question: How to avoid it ?

What I am looking for is a simple solution so that I can check

if( page was refresh ) //some condition


@iammark 2018-05-22 21:38:23

This can work in your scenario:

if(isset($_GET["token"])) {
    $view_token = $_GET["token"];
} else {
    $new_views = $views + 1;
    $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!

@Rizwan Siddiquee 2016-05-17 09:12:29

Best way to Detect Page Refresh. or Not ?(Ctrl+F5,F5,Ctrl+R, Enter)

$pageRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) &&($_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0' ||  $_SERVER['HTTP_CACHE_CONTROL'] == 'no-cache'); 
if($pageRefreshed == 1){
    echo "Yes page Refreshed";
    //enter code here
    echo "No";

@zero8 2016-12-14 09:26:18

A for the effort. ( =

@Jim 2014-06-18 07:28:15

    //here you get the url behind the domain.
    $currentPage = $_SERVER['REQUEST_URI']; 

    //if the session current page is not set.

      //set the session to the current page.
      $_SESSION['currentPage'] = $currentPage;     

    //check if the session is not equal to the current page
    if($_SESSION['currentPage'] != $currentPage){

       // if it's not equal you set the session again to the current page.
       $_SESSION['currentPage'] = $currentPage;

       //set the query you want to use

@Gideon Rosenthal 2014-07-15 20:16:56

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;

@Hafenkranich 2017-08-07 20:55:10

A POST request cant get "refreshed". It can only be send again. Thus chrome does this the right way and does not send the equivalent cache headers. @user1032531

@Sairam 2010-11-27 06:10:06

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 for information about CACHING

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

@deceze 2010-11-27 07:11:45

Agreed on the cookie, but when expressly refreshing a page, a browser will usually request the page from the server again regardless of cache settings.

@Moon 2010-11-27 06:19:28

i have solved the problem ... HURRAHHH with no session & no cookies

the solution is a combination of PHP : AJAX : JavaScript

the query that you want to run on Page Load & not on page Refresh run it as via AJAX call lets say my function for doing that is

function runQUERY()
    xmlhttp=new XMLHttpRequest();"POST","doIT.php",false);

and i can simply check with Javascript that the page is a fresh load or its a refresh by doing the following

<script type="text/javascript">
function checkRefresh()
    if( document.refreshForm.visited.value == "" )
        // This is a fresh page load
            alert ( 'Fresh Load' );
        document.refreshForm.visited.value = "1";
   you AJAX function here
        // This is a page refresh
        alert ( 'Page has been Refreshed, The AJAX call was not made');


<body onLoad="checkRefresh()">

<form name="refreshForm">
<input type="hidden" name="visited" value="" />


and in your doIT.php simple add your PHP code which you were going to put in the normal page

//put any code here, i won't run on any page refresh

@casablanca 2010-11-27 06:31:26

This assumes that the form values are retained after a refresh -- this is true in Firefox and IE but not in Chrome.

@Moon 2010-11-27 06:35:22

well then i think its Google's problem to match the steps while walking... and not going on doing a catwalk.. thinking oh i can do this because now i am very special

@Ben Regenspan 2010-11-27 06:54:39

I don't think this behavior you're seeing in FF and IE is part of any standard, and it could be subject to change, inconsistent between versions of those browsers, non-future-proof, etc. Cookie seems like a far more reliable solution.

@deceze 2010-11-27 07:09:38

Indeed, Chrome is not doing anything wrong, because there's no specified behavior for retaining form values on page refreshes/back/forward navigation. The correct way would be to present the page as intended (usually blank); retaining form values is something that some browsers may or may not do. Very interesting hack that works at least partly though.

@Sairam 2010-11-27 17:31:21

Looks like this does not work when javascript is not enabled.

@Jach Many 2013-05-28 06:26:29

The answer by the questionnaire is wrong and is misleading to the SO users.

@MERT DOĞAN 2017-05-25 05:44:49

You may never trust client side code. Your js and ajax code runs at client side.

@Diego 2018-02-28 03:10:44

client side code can easily be manipulated so its not 100% perfect

@casablanca 2010-11-27 05:17:18

You can't directly detect a page refresh, but you can use a cookie to simulate what you want:

if (isset($_COOKIE['action'])) {
  // action already done
} else {
  // run query

Depending on your requirements, you also need to decide when to remove the cookie and/or perform the action again.

@Rutger 2010-11-27 05:16:08

If you just want to run it once for a user, I would set a session cookie and then use an if() statement.


if (!$_SESSION['loaded'])
    // insert query here

$_SESSION['loaded'] = true;


@Moon 2010-11-27 05:29:00

once the $_SESSION['loaded'] is set i won't be able to make this check on any other page or on the same page with different parameters like you have the leech pages...unless the session is destroyed and i i destroy the session it means if my user is logged in, he\she will be logged out... and this syntax that you have given above will not work.... correct syntax is <?php session_start(); if (!isset($_SESSION['load']) ) { echo ("Go"); } $_SESSION['load'] = 1; ?>

@Moon 2010-11-27 06:21:09

i have solved it , i have posted the solution of this as answer to my own question..

Related Questions

Sponsored Content

28 Answered Questions

[SOLVED] How can I prevent SQL injection in PHP?

18 Answered Questions

[SOLVED] Reference — What does this symbol mean in PHP?

35 Answered Questions

[SOLVED] Reference - What does this error mean in PHP?

37 Answered Questions

[SOLVED] Deleting an element from an array in PHP

  • 2008-12-15 20:28:55
  • Ben
  • 2461522 View
  • 2374 Score
  • 37 Answer
  • Tags:   php arrays unset

27 Answered Questions

[SOLVED] How can I refresh a page with jQuery?

29 Answered Questions

[SOLVED] How do I get PHP errors to display?

7 Answered Questions

[SOLVED] How does PHP 'foreach' actually work?

15 Answered Questions

[SOLVED] Why shouldn't I use mysql_* functions in PHP?

  • 2012-10-12 13:18:39
  • Madara Uchiha
  • 212841 View
  • 2441 Score
  • 15 Answer
  • Tags:   php mysql database

31 Answered Questions

[SOLVED] startsWith() and endsWith() functions in PHP

  • 2009-05-07 12:14:27
  • Click Upvote
  • 807717 View
  • 1414 Score
  • 31 Answer
  • Tags:   php string

30 Answered Questions

[SOLVED] How do you parse and process HTML/XML in PHP?

Sponsored Content