Click to See Complete Forum and Search --> : Counter Question


comptech520
10-05-2006, 07:35 AM
Hello,

Does anyone see why this code would stop counting and reset at 2000?

create a file and name it count.dat
CHMOD count.dat to 666

<?

//set the count file
$counterfile = "count.dat";

//check the file is readable
if(!($fp = fopen($counterfile,"r"))) die ("cannot open counter file");

//grab the value and increase by 1
$count = (int) fread($fp, 20);
fclose($fp);
$count++;

//show the result, write the new value and close the file
echo "Site Hits: $count";
$fp = fopen($counterfile, "w");
fwrite($fp , $count);
fclose($fp);

?>

pcthug
10-05-2006, 07:53 AM
Works fine for me, even with count.dat at 666.

Probably a Y2K bug or something... :rolleyes:

netbuddy
10-05-2006, 08:19 AM
Possibly this is why http://uk2.php.net/manual/en/function.fread.php your reading the first 20 bits of data in the stream according to the link I posted, so your counter could be exceeding the 20 bits your telling it to read, if you look at the example the addition of filesize() may help.

NogDog
10-05-2006, 05:13 PM
I'd suggest using file_get_contents() to read the file:

$counterfile = "count.dat";
if(!is_readable($counterfile))
{
die ("cannot read counter file");
}
$counter = (int) trim(file_get_contents($counterfile));
//...rest of script...