skip to Main Content

I am having this strange issue and can’t figure it out.

On some websites I have this script works perfect… same code, same server settings…

With php, there is a simple page view hit counter that stores locally in a txt file.

Then I echo out the value on the footer copyright area of my websites to give the client a quick statistic… its pretty cool how fast it grows.

Anyway.. i have a client corner grill ny . com (seo purposes I added spaces )

On that website.. its been working great for years.

Now another website and a bunch more.. for example… savianos . com

This breaks.. and the text value is blank.

This is the counter.php code

 <?php
session_start();
$counter_name = "counter/hits.txt";

//Check if a text file exists. If not create one and initialize it to zero.
if (!file_exists($counter_name)) {
  $f = fopen($counter_name, "w");
  fwrite($f,"0");
  fclose($f);
}

// Read the current value of our counter file
$f = fopen($counter_name,"r");
$counterVal = fread($f, filesize($counter_name));
fclose($f);

// Has visitor been counted in this session?
// If not, increase counter value by one
if(!isset($_SESSION['hasVisited'])){
  $_SESSION['hasVisited']="yes";
  $counterVal++;
  $f = fopen($counter_name, "w");
  fwrite($f, $counterVal);
  fclose($f); 
}

?>

Now, if I add a value in the txt file.. like 1040… and go to the website it starts to work… then after a week or so I check it .. its blank again.

Any ideas?

I am thinking that this may be happening because the website might get a TON of views during dinner time friday night.. and the simple script can’t handle it so.. while its trying to write a added a number it just breaks and go to blank.. and never starts back up again.

The structure is this.

/counter/ folder has
counter.php and a hits.txt file

Every page of the website the very first thing is

 <?php include ('counter/counter.php'); ?>

and in the footer of the website we have

 <?php echo $counterVal; ?>

3

Answers


  1. Your code looks perfect, but let’s understand the situation. You have a file which can be accessed concurrently for many users, because page visit can be done by multiple users on same time. This does’t seem right you have to lock the file manipulation for another user while someone is modifying it, right?. Please have a look

    Visits counter without database with PHP

    Login or Signup to reply.
  2. It is most likely because you have two concurrent scripts that tried to open the file at one and one of them fail. You have to use flock() when there are multiple instances of the script that could operate at the same time. Counter are some of the heaviest things if you going to use file reading and writing. I wrote this wrapper to easily implement file locking.

    If you want to check out one of my counters that in operation try http://ozlu.org. That dynamic counter image was self-built. The fileReadAll will read the entire file in one shot. The file writer only has two modes, write or append. You can pass the fileWriter an array or a string and it will write it to the file. The function will not add any n to format your text so you would have to add that. The default mode for the fileWriteAll is w if you do not set the third argument.

    function fileWriteAll($file, $content, $mode = "w"){
        $mode = $mode === "w" || $mode === "a"? $mode : "w";
        $FILE = fopen($file, $mode);
            while (!flock($FILE, LOCK_EX)) { usleep(1); }
            if( is_array($content) ){
                for ($i = 0; $i < count($content); $i++){
                    fwrite($FILE, $content[$i]);
                }
            } else {
                fwrite($FILE, $content);
            }
         flock($FILE, LOCK_UN);
         fclose($FILE);
    }
    
    function fileReadAll($file){
        $FILE = fopen($file, 'r');
            while (!flock($FILE, LOCK_SH)) { usleep(1); }
            $content = fread($FILE, filesize($file));
            flock($FILE, LOCK_UN);
        fclose($FILE);
        return $content;
    }
    
    Login or Signup to reply.
  3. Your modified code:

    session_start();
    
    $counterName = './views.txt';
    
    if (!file_exists($counterName)) {
        $file = fopen($counterName, 'w');
        fwrite($file, '0');
        fclose($file);
    }
    
    $file = fopen($counterName, 'r');
    $value = fread($file, filesize($counterName));
    fclose($file);
    
    
    if (! isset($_SESSION['visited'])) {
        $_SESSION['visited'] = 'yes';
        $value++;
        $file = fopen($counterName, 'w');
        fwrite($file, $value);
        fclose($file);
    }
    
    session_unset();
    
    
    
    echo $value;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search