• Welcome to Valhalla Legends Archive.
 

Crude PHP Counter

Started by j0k3r, January 18, 2004, 06:46 AM

Previous topic - Next topic

j0k3r

I'm trying to build a PHP counter (not very good, but it'll do the job) and can't figure out what's going wrong. Basically it reads the number (what the counter is at) from a text file and increments it, puts it to the screen, writes the updated number back to the text file and closes it.


<?php
/*source of file*/
$filename 'current.txt';
/*open the file (r+ for read+write)*/
$fp fopen($filename,'r+');
/*read the file and store it in $currentval (1 line)*/
$currentval fread($fpfilesize($filename));
/*increment the value (because it's a counter)*/
$currentval++;
/*write it to the screen*/
echo $currentval;
/*write the incremented value to the source file*/
fwrite ($fp$currentval);
/*close the file*/
fclose ($fp);
?>



No errors are generated from the code, and it just seems as though fwrite() isn't working (I'm probably not using it properly). Online Function Reference at: http://ca.php.net/function.file
QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo

j0k3r

I tried uploading, and it worked sort of, ended up fixing it eventually... For those interested:
<?php
$filename 
'current.txt';   /*source of file*/   
$fp fopen($filename,'r+');   /*open the file (r+ for read+write)*/   
flock($fp2);         /*lock the file*/   
$currentval fread($fpfilesize($filename));   /*read the file and store it in $currentval (1 line)*/
$currentval++;               /*increment the value (because it's a counter)*/   
echo $currentval;      /*write it to the screen*/
fseek($fp,0);         /*go to beginning of file*/   
fputs ($fp$currentval);   /*write the incremented value to the source file*/   
flock($fp3);         /*unlock the file*/   
fclose ($fp);         /*close the file*/   
?>

QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo