Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 29 Feb 2004 14:08:57 +0000
From:      Peter Risdon <peter@circlesquared.com>
To:        fbsd_user@a1poweruser.com
Cc:        "freebsd-questions@FreeBSD. ORG" <freebsd-questions@FreeBSD.ORG>
Subject:   Re: Counter in php script
Message-ID:  <4041F279.1040206@circlesquared.com>
In-Reply-To: <MIEPLLIBMLEEABPDBIEGCEEKFNAA.fbsd_user@a1poweruser.com>
References:  <MIEPLLIBMLEEABPDBIEGCEEKFNAA.fbsd_user@a1poweruser.com>

next in thread | previous in thread | raw e-mail | index | archive | help
fbsd_user wrote:

>Need to set the initial value for an counter and save it, then 
>bump the saved value by one every time the php script is executed. 
>This must be a very basic function, but not being an php 
>script coder my self this is all new to my.
>
>Can anyone provide sample code I can use to do this?
>  
>
 From a user contribution to the php manual at
http://www.php.net/manual/en/function.fopen.php

- edit to suit and use at your own risk.

PWR.

|$counter_file = '/tmp/counter.txt';
clearstatcache();
ignore_user_abort(true);    ## prevent refresh from aborting file 
operations and hosing file
if (file_exists($counter_file)) {
   $fh = fopen($counter_file, 'r+');
   while(1) {
     if (flock($fh, LOCK_EX)) {
         #$buffer = chop(fgets($fh, 2));
         $buffer = chop(fread($fh, filesize($counter_file)));
         $buffer++;
         rewind($fh);
         fwrite($fh, $buffer);
         fflush($fh);
         ftruncate($fh, ftell($fh));   
         flock($fh, LOCK_UN);
         break;
     }
   }
}
else {
   $fh = fopen($counter_file, 'w+');
   fwrite($fh, "1");
   $buffer="1";
}
fclose($fh);

print "Count is $buffer";

?>|




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4041F279.1040206>