Re: Counter in php script

2004-02-29 Thread Peter Risdon
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;

?|

___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Counter in php script

2004-02-29 Thread Peter Risdon
Peter Risdon wrote:

whoops... delete junk |characters inserted by my paste:

|$counter_file = '/tmp/counter.txt';
 ^

and



?|
^

PWR

___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


RE: Counter in php script

2004-02-29 Thread fbsd_user
Thanks, a really great reply.
Have some questions about the fine details.

The counter file is an single line with any size numeric
field starting in position 1?
Like this, right?
200400

Does the file have to be an  .txt  file?

What happens if the counter file is in use and locked?
Does the second request pause until file is free?

If the counter file has no path prefix, then it's looked for
in the same location as the script, right?
$counter_file = 'counter.php'

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Behalf Of Peter
Risdon
Sent: Sunday, February 29, 2004 9:09 AM
To: [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED] ORG
Subject: Re: Counter in php script

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;

?|


___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to
[EMAIL PROTECTED]

___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Counter in php script

2004-02-29 Thread Peter Risdon
fbsd_user wrote:

Thanks, a really great reply.
Have some questions about the fine details.
 

I haven't used this script, just happened to notice it the other day 
while looking something up, but ...

The counter file is an single line with any size numeric
field starting in position 1?
Like this, right?
200400
 

Looks like it. If the file isn't there already, the script creates it.

Does the file have to be an  .txt  file?
 

It can have any name you like, if that's what you mean. The contents are 
always going to be the same.

What happens if the counter file is in use and locked?
Does the second request pause until file is free?
 

File locking is OS dependent. More particularly, php file locking works 
fine with other attempts to access the file from thingies that are also 
using the same type of file locking as php. So, I believe the 
conservative answer here is: yes, on FreeBSD, at least so far as other 
attempts from php scripts to access the file are concerned.

If the counter file has no path prefix, then it's looked for
in the same location as the script, right?
$counter_file = 'counter.php'
 

Yes, though personally I prefer to be anal and state the full path. If 
you might use the file in a number of locations in an expanded script 
and need to refer to this path more than once, set it once as:

$filepath = '/usr/home/someuser/counters/';

and edit just that line when you need to, then use:

$counter_file = $filepath.counter.php;

or similar throughout the script. Bear in mind that the file will be 
written as the default apache user, probably www, so the directory needs 
appropriate permissions.

PWR.

___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]