PDA

View Full Version : Php Counter


Comglomo
02-17-2003, 07:21 PM
Can anyone give me the code to make a simple counter.. that just desplays the numbers as text.. (not a picture)
Thanks

<htmlite>
02-17-2003, 11:37 PM
Something like this? (php)


<?
$counter_file = "./counter.dat";
if(!($fp = fopen($counter_file, "r"))) die ("Cannot open file.");
$counter = fread($fp, 25);
fclose($fp);
echo "$counter";
?>

tonto
02-18-2003, 12:25 AM
place this code at the top of the file u want to have count

<?
$counter_file = "./counter.dat";
if(!($fp = fopen($counter_file, "r+"))) die ("Cannot open file.");
$counter = fread($fp, 25);
$counter++ ;
fwrite($fp,$counter) ;
fclose($fp);
echo "$counter";
?>
or u can put this code in a seperate file and include() this file into the file u want to have counter on.

Comglomo
02-18-2003, 09:02 AM
whenever i put this code in, i preview it on a blank php page
12.226.40.198/phpinfo.php (http://12.226.40.198/phpinfo.php)
It says
Warning: fopen(./counter.dat) [function.fopen]: failed to create stream: No such file or directory in c:\program files\apache group\apache\htdocs\phpinfo.php on line 3
Cannot open file.

<htmlite>
02-18-2003, 09:27 AM
oops, sorry,
the one I posted just reads the file.
the one tonto posted is the more correct one to use.

and for the error part, you will have to create a blank page and name it "counter.dat". upload it to the same area as your script and it should be ok.

tonto
02-18-2003, 09:38 AM
Warning: fopen(./counter.dat) [function.fopen]: failed to create stream: No such file or directory in c:\program files\apache group\apache\htdocs\phpinfo.php on line 3
counter.dat is the file ur storing ur counts.
make sure it exists in the right directory
eg not to make things complecated just create a file counter.dat in the same directory where ur script is open it using $counter_file = "counter.dat";
tonto