Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

292
Views
simple php page session visit counter not working

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; ?>
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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

over 4 years ago · Santiago Trujillo Report

0

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;
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!