Jump to content

Weird results from hit counter file


garyed

Recommended Posts

I have this php counter file that is included in my website index page to check how many hits I get &  put it into a separate file. It works fine for about a week or so and then it looks like the number gets multiplied by 10. Below is the code and the resulting file it produces. Any ideas why this is happening?

 

  $tailer=`tail -n 1 counter.txt `; /* gets a string of only the last line of the file */
$today= date("D-m/d/Y"); /* gets todays date */
$my_array=explode(" ",$tailer); /* puts the string into an array */
$old_day= $my_array[0]; /* original date from the last line of the file */
$old_hits=$my_array[2]; /* original amount of hits from last line */
$new_hits=$old_hits +1; /* add one to the hit counter */
$my_new_array=array("$old_day","hits","$new_hits"); /* makes the new array */
$newstr=implode(" ",$my_new_array); /* puts array back into a string */  
$filename="counter.txt";
/* If the date is the same then the adjusted line will overwrite the last line */
 if ($today == $old_day) {
$lines = file($filename); /* get the contents of the file into an array */
$all_lines = implode('',$lines);  /* puts the array into a string */
$fp=fopen($filename,"w");   /* opens file for writing */
$entry=str_replace("$tailer","$newstr",$all_lines);  /* the command to search & replace */  
$fw=fwrite($fp,$entry); /* writes to the file */
fclose($fp);  /* closes the file */
}
else {
/* Or else the date changed so it needs to start a new line   */
$newline="\n"; 
$new_day_array=array("$today","hits","1"); 
$new_day=implode(" ",$new_day_array); /* Puts the new day and first hit on a line */ 
$fp=fopen($filename,"a");
$fw=fwrite($fp,$newline); /* needed to change line when date changes */
$fw=fwrite($fp,$new_day);
fclose($fp);
}
 

 

Here is the resulting file it produces:

 


 

Tue-12/24/2013 hits 204
Wed-12/25/2013 hits 197
Thu-12/26/2013 hits 241
Fri-12/27/2013 hits 262
Sat-12/28/2013 hits 230
Sun-12/29/2013 hits 2043
Sun-12/29/2013 hits 204
Mon-12/30/2013 hits 326
Tue-12/31/2013 hits 251
Wed-01/01/2014 hits 229
Thu-01/02/2014 hits 355
Fri-01/03/2014 hits 2500
Fri-01/03/2014 hits 250
Sat-01/04/2014 hits 299
Sun-01/05/2014 hits 252
Mon-01/06/2014 hits 358
Tue-01/07/2014 hits 401
Wed-01/08/2014 hits 427
Thu-01/09/2014 hits 408
Fri-01/10/2014 hits 1509
Fri-01/10/2014 hits 15
Sat-01/11/2014 hits 276
Sun-01/12/2014 hits 52
Link to comment
Share on other sites

Your code could be improved quite a bit. For instance there is no need to execute the tail command, and you can streamline your processing into less code.

<?php

//First we read in all lines
$lines = file('counter.txt', FILE_IGNORE_NEW_LINES);

//Then get the last line from the array of lines
$last = $lines[count($lines)-1];

//Split the line into the interesting parts.
list($day, ,$hits) = explode(' ', $last);

//If the day matches today, increase hits and overwrite, otherwise add a new entry
$today = date("D-m/d/Y");
if ($day == $today){
   $hits++;
   $lines[count($lines)-1] = "{$day} hits {$hits}";
}
else {
   $lines[] = "{$today} hits 1";
}

//Re-write the file
file_put_contents('counter.txt', implode(PHP_EOL, $lines));
You'll need to add your own validations/error handling such as what to do if the file doesn't exist or is empty.

 

 

As for your large count, have you checked your server's access log? You might just be getting hit by a bot or two that day causing a large number of hits.

Link to comment
Share on other sites

Your code could be improved quite a bit. For instance there is no need to execute the tail command, and you can streamline your processing into less code.

<?php

//First we read in all lines
$lines = file('counter.txt', FILE_IGNORE_NEW_LINES);

//Then get the last line from the array of lines
$last = $lines[count($lines)-1];

//Split the line into the interesting parts.
list($day, ,$hits) = explode(' ', $last);

//If the day matches today, increase hits and overwrite, otherwise add a new entry
$today = date("D-m/d/Y");
if ($day == $today){
   $hits++;
   $lines[count($lines)-1] = "{$day} hits {$hits}";
}
else {
   $lines[] = "{$today} hits 1";
}

//Re-write the file
file_put_contents('counter.txt', implode(PHP_EOL, $lines));
You'll need to add your own validations/error handling such as what to do if the file doesn't exist or is empty.

 

 

As for your large count, have you checked your server's access log? You might just be getting hit by a bot or two that day causing a large number of hits.

 

Thanks for the ideas,

I hadn't thought about getting hit by a bot but that makes a lot of sense because I use the same counter file on another page of my site & never have any problems. It just produces a different .txt file so i can see how many users go to my home page & then follow through to another page on my site.

I'm on a shared server & don't know how to check the access log or to be honest, what it even is.   

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.