0

I've placed a hit counter on my page. It reads a text file, increments the number in the file, and later in the page, I output the incremented value.

$hitsFile   = "hits/exps/stats.txt";
$hits       = file($hitsFile);
$hits[0]++;
$fp = fopen($hitsFile , "w");
flock($fh, LOCK_EX);
fwrite($fp , $hits[0]);
fclose($fp);

My problem is that if I reload the page, the code will increment the hits. I don't want that. I thought of using session to fix that, but with session, in order the increment the hits again, I need to exit the site and visit again. I don't want that either.

I want it to increment not when I reload the page but when I revisit the page.

For example, let's say I have two-page website, Home and Contact, and on contact page I have a hit counter. I don't want the hit counter to increment if I reload(refresh) the contact page, but if I leave the contact page and visit homepage, and later revisit the contact page, I want it to increment.

In short, I don't want it to increment on page reload. Is there a way to do that?

akinuri
  • 10,690
  • 10
  • 65
  • 102

4 Answers4

2

In each of your pages, you need to write the page name in the session.
Do something like this:

$_SESSION['page'] = 'contact';

On the pages where you need to count hits, you need to check this session key.
For example, if you were on page 'contact', then $_SESSION['page'] == 'contact'.
Now when you go to visit the 'homepage':

$page = $_SESSION['page'];
if($page != 'homepage')
{
    //increment your hits counter
    $_SESSION['page'] = 'homepage';
}
SherylHohman
  • 16,580
  • 17
  • 88
  • 94
fiction
  • 566
  • 1
  • 6
  • 21
  • 1
    What if I'm using hit counter in all pages to store hits for each page? – akinuri Aug 09 '14 at 19:46
  • @akinuri so you will need to add second code (it will check reload it or not) provided by me on the top of each of pages. **And note, that you need to change comment** `//increment your hits counter` **to code, that increments youe counter, actually.** Also you need to change name of pages (in the example i used _homepage_ name) – fiction Aug 09 '14 at 19:52
1

I suggest this method, is my preferred, create in root these folders: cnt and log... then put inside cnt folder the following files cnt.php and showcnt.php...

cnt.php

<?php
  ##############################################################################
  # Php Counter With Advanced Technology For The Prevention Of Reloading Pages #
  # Version: 1.4 - Date: 13.11.2014 - Created By Alessandro Marinuzzi [Alecos] #
  ##############################################################################
  function cnt($file) {
    session_start();
    global $pagecnt;
    $reloaded = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';
    $thispage = basename($_SERVER['SCRIPT_FILENAME']);
    if (!isset($_SESSION['first_go'])) {
      $_SESSION['first_go'] = 1;
      $first_go = TRUE;
    } else {
      $first_go = FALSE;
    }
    if (!isset($_SESSION['thispage'])) {
      $_SESSION['thispage'] = $thispage;
    }
    if ($_SESSION['thispage'] != $thispage) {
      $_SESSION['thispage'] = $thispage;
      $new_page = TRUE;
    } else {
      $new_page = FALSE;
    }
    $pagecnt = rtrim(file_get_contents($file));
    if ((!$reloaded) && ($new_page == TRUE) || ($first_go == TRUE)) {
      $fd = fopen($file, 'w+');
      flock($fd, LOCK_EX);
      fwrite($fd, ++$pagecnt);
      flock($fd, LOCK_UN);
      fclose($fd);
    }
  }
?>

showcnt.php

<?php
  ##############################################################################
  # Show Counter Results - v.1.4 - 13.11.2014 By Alessandro Marinuzzi [Alecos] #
  ##############################################################################
  function gfxcnt($file) {
    global $number;
    $number = rtrim(file_get_contents($file));
    $lenght = strlen($number);
    $gfxcnt = "";
    for ($i = 0; $i < $lenght; $i++) {
      $gfxcnt .= $number[$i];
    }
    $gfxind = "<span class=\"counter\"><span class=\"number\">$gfxcnt</span></span>";
    echo $gfxind;
  }
?>

Well, then edit your index.php or other php page... and put at the beginning this piece of code:

<?php session_start(); include("cnt/cnt.php"); cnt("log/index.txt"); include("cnt/showcnt.php"); ?>

Well, then edit index.php or other php page... and use this piece of code for reading counter file:

<?php gfxcnt("log/index.txt"); ?>

It's all, I hope you'll find my answer useful :) My counter can write/read multiple php pages...

Source: my blog (https://www.alecos.it/new/101/101.php)

Alessandro
  • 900
  • 12
  • 23
0
  1. Update the hit count only if the current URL is not stored in $_SESSION['url'].
  2. After updating the hit count, store the current URL in $_SESSION['url'].
Oswald
  • 31,254
  • 3
  • 43
  • 68
0

Add session_start(); to the top.

Now change your if to this:

if (!isset($_SESSION['lastpage']) || $_SESSION['lastpage'] != $_SERVER['QUERY_STRING') {
   $hits[0]++;
}

$_SESSION['lastpage'] = $_SERVER['QUERY_STRING'];

This will basically force someone to move to another page if they want to increment the counter.

David Xu
  • 5,555
  • 3
  • 28
  • 50