From: "Hartley, Matt" <[EMAIL PROTECTED]> > I am trying to have a counter in a file. With what I have below I get in > the $counterfile > > 0 + 01 = 01 > 01 + 01 = 012 > 012 + 01 = 01213 > > [snip] > > /* Add "1" to the counter */ > $counter = ($counter + '01');
You're "adding" a string. That doesn't make much sense. Just use $counter++; or $counter = $counter + 1; > /* Verify counter file is writable */ > if (is_writable($counterfile)); /* { */ > if (!$handle = fopen($counterfile, 'a')) { > print "Cannot open file ($counterfile)"; > exit; > } > /* Write to our counter file. */ > if (!fwrite($handle, $counter)) { > print "Cannot write to file ($counterfile)"; > exit; > } > fclose($handle); > > [/snip] > How do I overwrite the existing value with my new value instead of it adding > on after the past values? Open the file in "w" (write) mode instead of "a" (append) mode. This will clear the file and you can write your new value to it. Note that if two peope hit your site at the same time, you'll have issues here because they'll both try to open and read the file. You could end up missing hits. Why not approach it this way, which I've seen mentioned on here. Instead of keeping an actual number in the file, just write a character to the file. You can then use filesize() to determine how many "hits" are in the file. <?php $fp = fopen('counter.txt','a'); fwrite($fp,'1'); fclose($fp); echo 'This page viewed ' . filesize('counter.txt') . ' times.'; ?> > this counter decides what line to display in a sequence from a separate file > what command can I use to tell it when $counter is @ the last line in my > "tips" file to reset to zero I don't understand what you're asking here. ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php