Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Detect Page Refresh

Tags:

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

<?php
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
{
 do
}
like image 951
Moon Avatar asked Nov 27 '10 05:11

Moon


People also ask

How can I tell if jquery is refreshing a page?

On Refresh/Reload/F5: If user will refresh the page, first window. onbeforeunload will fire with IsRefresh value = "Close" and then window. onload will fire with IsRefresh value = "Load", so now you can determine at last that your page is refreshing.


2 Answers

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;
}
like image 165
Gideon Rosenthal Avatar answered Sep 18 '22 16:09

Gideon Rosenthal


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";
}else{
    //enter code here
    echo "No";
}
like image 25
Rizwan Siddiquee Avatar answered Sep 22 '22 16:09

Rizwan Siddiquee