Hi,

I borrowed some code I found (I believe on this site) for a php counter:

It is resetting the count at the beginning of each month, and I am not seeing where this happens.

$file = "hits/hits.txt";
$open = fopen($file, "r");
$size = filesize($file);
$count = fread($open, $size);
$count1 = $count+1;
fclose($open);

$file1 = "hits/ip.txt";
$lines = file($file1);

foreach ($lines as $line_num => $line)
{
$firstPos = strpos($line,'"');
$secPos = strpos($line,'"');
$ddate = substr($line,$firstPos+1,($secPos-$firstPos)-2);
break;
}

$dat = date('d');
$rip = $_SERVER['REMOTE_ADDR'];

if($dat != $ddate)
{
    $open2 = fopen($file1, "w");
    fwrite($open2, "DATE= \"$dat\"");
    fwrite($open2, "\n\n$rip");
    fclose($open2);

$open = fopen($file, "w");
fwrite($open, $count1);
fclose($open);
$count = $count1;
}
else
{
    $open2 = fopen($file1, "r");
    $size = filesize($file1);
    $ips = fread($open2, $size);

if(strpos($ips,$rip))
{
}
else
{
    $open = fopen($file, "w");
    fwrite($open, $count1);
    fclose($open);
    $count = $count1;

    $open3 = fopen($file1, "a");
    fwrite($open3,"\n\n");
    fwrite($open3, $rip);
    fclose($open3);
}
fclose($open2);

}

    it is just comment out... most likely it was used for debug purposes

    if u want a counter script that counts unique hits by IP address perday then i can provide u with one ready to go...

      I actually am just really interested in understanding at what point in this code does it determine it's a new month and rewrites the 'hits.txt' file starting at 0.

      -patrick

        Sheesh, that's ugly. To answer your question:

        patrick99e99 wrote:

        at what point in this code does it determine it's a new month and rewrites the 'hits.txt' file starting at 0.

        It's

        if($dat != $ddate)

        And it changes files on a daily basis.

          Hmm.. from what I can see, that's not right...

          It's saying: if the date in the ip address file (ip.txt) is the same as the actual month date (day), then append the current ip address ($rip) to the IP address file.. However, if the dates are not equal then start with a clean ip.txt file.. but the other .txt file ($file) is always replaced with $count1 which is defined at the very beginning...

          $file = "hits/".$filename;
          $open = fopen($file, "r");
          $size = filesize($file);
          $count = fread($open, $size);
          $count1 = $count+1;
          fclose($open);
          

          I don't see any place where $count is reset.. which is why I am confused... this actually started back at 1 on my site today as it's March 1st...

          -patrick

            It's because there is nothing that bothers to look at what month it is when it writes a new file, only the day of the month. That's why the files say "DATE = 1". It can't tell the difference between February 1 and March 1, only that today is different from yesterday and so it should start a new file. It doesn't care that there might be another file for "DATE = 1" sitting around already.

            It's all kind of pointless when you remember that the web server keeps much more detailed (and robust) logs anyway....

              But what I'm saying is each time it rewrites the first file ($file), it is writing it with the value of $count1 (which is the value written to $file + 1)... So there never is a place where is set to $count=1...

              Regardless of what the file with "DATE = " says... I mean, when I analyze everything, that's how I see it.. Am I really on crack?

                Write a Reply...