Moon Moon - 8 months ago 37
PHP Question

PHP: Detect Page Refresh

I have a page

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


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