php - How can I identify refresh from any other type page change

108 Views Asked by At

I am using php code to create and update an SQL database which I, in turn, use to populate an on-screen page counter (see code snippet below). How can I differentiate a page refresh from any other url change so as not to add to my counter every time the page is refreshed? I want to do this without putting cookies on my user's desktop. Thanks in advance for your help.

include("incPHP1.inc");
$cxn=mysqli_connect($host,$user,$password,$database)
    or die ("Couldn't connect to the server.");
$result = mysqli_query($cxn, "SELECT * FROM visitors");
$counter = mysqli_num_rows($result);
$views = $counter;

Cosmin:

I copied the code as you have written it into the first line of my php code (see first attempt code below). It did not work in either IE11 or Firefox 55.0.3. In both cases, I got an error message (Fatal error: Function name must be a string in D:\InetPub\vhosts\rlcdev.com\httpdocs\index.php on line 2) and no output. I removed the initial $session_name(); command line and left it out (see second attempt code below). This was because I understand that the $session_start(); command must be the first line after the

First Attempt:

<?php
$session_name();
$session_start();
$_SESSION['refresh'] = isset($_SERVER['HTTP_CACHE_CONTROL'])  && $_SERVER

['HTTP_CACHE_CONTROL'] === 'max-age=0';
if($_SESSION['refresh'])
{
            echo ("<SCRIPT>alert ( 'Page has been refreshed, The AJAX call was not made');</SCRIPT>");
}
else
{
                echo ("<SCRIPT>alert ( 'This is a non-refresh reload' );</SCRIPT>");
}

Second Attempt:

<?php
$session_start();
$_SESSION['refresh'] = isset($_SERVER['HTTP_CACHE_CONTROL'])  && $_SERVER

['HTTP_CACHE_CONTROL'] === 'max-age=0';
if($_SESSION['refresh'])
{
            echo ("<SCRIPT>alert ( 'Page has been refreshed, The AJAX call was not made');</SCRIPT>");
}
else
{
                echo ("<SCRIPT>alert ( 'This is a non-refresh reload' );</SCRIPT>");
}
3

There are 3 best solutions below

0
On

Use cookies.

In your case set a cookie with name you prefer and value you prefer (already_visited = 1) with lifetime validity (7 years). Then you can check if this cookie is sent by browser. If browser has sent the cookie value then it is a refresh else if the cookie value is not sent by browser then it is a first time visit.

Note: This technique is dependent on cookies, you can only track if the cookies are accepted in browser. There is no option to check this if cookies are disabled in the browser.

0
On

Still didn't work, Cosmin. I've decided to go a different way, using a SQL database, timer and Ajax calls to determine if the call is a refresh or new. It'll help with some other issues I'm trying to address. Thanks for all the help.

7
On

Here is another method: use a flag variable

$refresh = isset($_SERVER['HTTP_CACHE_CONTROL'])  && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';
if($refresh)
{
  echo ("<SCRIPT>alert ( 'Page has been refreshed, The AJAX call was not made');</SCRIPT>");
}
 else
     {
      echo ("<SCRIPT>alert ( 'This is a non-refresh reload' );</SCRIPT>");
     }