I write a piece of code to tracking the number of clicks on a link
each day. The link usu. gets about 3000-6000 clicks per day. I just
store today's click count in a one-line text file, like,
01/07/2004 1234
The first field is the date and the second field is number of clicks.
I just read the current count, increase by 1 and then write back. When
another day comes, I write today's count to mysql DB.
The problem I am having is that sometimes during a single day, the
click count seems to be reset to zero and starts counting from zero
again. Normally I'd get at least 3000 clicks a day. But sometimes, the
clicks I've got are only around 600. I am sure there is something
wrong with my code. I've pasted my code. I'd appreciate if sombody can
help me with some tips.
$hitcount=0;
$today=date("m/d/Y");
$hitdate=$today ;
$fdaycount="day count.txt";
if (file_exists($f daycount))
{
//read out today's count from file
$handle=fopen($ fdaycount,"r");
if (!$handle) exit(0);
if (!feof($handle) )
{
$s=fgets($handl e);
$p=strpos($s," ");
$hitdate=substr ($s,0,$p);
$hitcount=(inte ger)substr($s,$ p+1);
}
fclose($handle) ;
}
if ($hitdate==$tod ay)
{
//increase todays' count by 1
$hitcount++;
$fp=fopen($fday count,"w");
if (!$fp) exit(0);
$s=$today." ".$hitcount ;
fputs($fp,$s);
fclose($fp);
}
else
{
//write today's data into database
.....
//
//reset today's count to 1
$fp=fopen($fday count,"w");
if (!$fp) exit(0);
$s=$today." 1";
fputs($fp,$s);
fclose($fp);
}
each day. The link usu. gets about 3000-6000 clicks per day. I just
store today's click count in a one-line text file, like,
01/07/2004 1234
The first field is the date and the second field is number of clicks.
I just read the current count, increase by 1 and then write back. When
another day comes, I write today's count to mysql DB.
The problem I am having is that sometimes during a single day, the
click count seems to be reset to zero and starts counting from zero
again. Normally I'd get at least 3000 clicks a day. But sometimes, the
clicks I've got are only around 600. I am sure there is something
wrong with my code. I've pasted my code. I'd appreciate if sombody can
help me with some tips.
$hitcount=0;
$today=date("m/d/Y");
$hitdate=$today ;
$fdaycount="day count.txt";
if (file_exists($f daycount))
{
//read out today's count from file
$handle=fopen($ fdaycount,"r");
if (!$handle) exit(0);
if (!feof($handle) )
{
$s=fgets($handl e);
$p=strpos($s," ");
$hitdate=substr ($s,0,$p);
$hitcount=(inte ger)substr($s,$ p+1);
}
fclose($handle) ;
}
if ($hitdate==$tod ay)
{
//increase todays' count by 1
$hitcount++;
$fp=fopen($fday count,"w");
if (!$fp) exit(0);
$s=$today." ".$hitcount ;
fputs($fp,$s);
fclose($fp);
}
else
{
//write today's data into database
.....
//
//reset today's count to 1
$fp=fopen($fday count,"w");
if (!$fp) exit(0);
$s=$today." 1";
fputs($fp,$s);
fclose($fp);
}
Comment