Forum Moderators: coopster

Message Too Old, No Replies

PHP counter

         

Richie Graham

9:48 pm on May 9, 2003 (gmt 0)

10+ Year Member



Hi yes sorry it's me again...

I've got a counter script which I wrote, but it doesn't seem to be changing at all, just staying at 1. The code is split onto two pages as I have a page loading in a iframe in a page. The background page has the code for incrementing the counter, and the iframe page has the code for displaying the counter. It's displaying the count correctly, its just the wrong count! The code is as follows:

CODE FOR INCREMENTING COUNTER:


$counterfile = fopen("counter.txt","r"); // opens the counter file for wtiting to

$counternumber = fread($counterfile,6);

$counternumber = $counternumber + 1;

fwrite ($counterfile, $counternumber);

fclose ($counterfile);

CODE FOR DISPLAYING COUNTER:


$fn = "counter.txt";
$fh = fopen ($fn , "r");
$count = fread ($fh, filesize($fn));
fclose ($fh);
print ("<b><div style=\"position: absolute; left: 525px; top: 10px; z-index: 6; font size: 24pt; font-family: Verdana, Arial, sans-serif; color: #000080\">$count</div>\n");
print ( "<div style=\"position: absolute; left: 527px; top: 12px; z-index: 5; font size: 24pt; font-family: Verdana, Arial, sans-serif; color: #CCCCCC\">$count</div></b>");

// The two <div> tags should give the text a shadow effect

jatar_k

2:00 am on May 10, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



fopen [php.net]("counter.txt","r")

you are opeming for read only. use w+ or r+. I would imagine yor fwrite is failing.

fwrite() [php.net] returns the number of bytes written, or FALSE on error.

try
$success = fwrite ($counterfile, $counternumber);
if ($success == false) {
echo "oops";
}

bet it will say oops ;)

daisho

2:47 am on May 10, 2003 (gmt 0)

10+ Year Member



you may also want to add an fflush($counterfile); before your fclose to ensure your counter is flushed to disk. Not needed but more of a safty step incase the number is important.

daisho.

Richie Graham

8:48 am on May 10, 2003 (gmt 0)

10+ Year Member



That's probably the reason... I feel kinda stupid asking now it was such a simple thing like that... I had in mind something like the fact that it was reading the file as a string not an integer or something like that so couldn't increment it!

Thanks for both your help

GBU

Richie