From owner-freebsd-questions@FreeBSD.ORG Sun Feb 29 07:01:25 2004 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 1FFA816A4CE for ; Sun, 29 Feb 2004 07:01:25 -0800 (PST) Received: from mta9.adelphia.net (mta9.adelphia.net [68.168.78.199]) by mx1.FreeBSD.org (Postfix) with ESMTP id C449443D1F for ; Sun, 29 Feb 2004 07:01:24 -0800 (PST) (envelope-from fbsd_user@a1poweruser.com) Received: from barbish ([67.20.101.119]) by mta9.adelphia.net (InterMail vM.5.01.06.05 201-253-122-130-105-20030824) with SMTP id <20040229150124.XYDU25917.mta9.adelphia.net@barbish>; Sun, 29 Feb 2004 10:01:24 -0500 From: "fbsd_user" To: "Peter Risdon" Date: Sun, 29 Feb 2004 10:01:23 -0500 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook IMO, Build 9.0.6604 (9.0.2911.0) In-Reply-To: <4041F279.1040206@circlesquared.com> X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 Importance: Normal cc: "freebsd-questions@FreeBSD. ORG" Subject: RE: Counter in php script X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list Reply-To: fbsd_user@a1poweruser.com List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 29 Feb 2004 15:01:25 -0000 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? 2004000000 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: owner-freebsd-questions@freebsd.org [mailto:owner-freebsd-questions@freebsd.org]On Behalf Of Peter Risdon Sent: Sunday, February 29, 2004 9:09 AM To: fbsd_user@a1poweruser.com Cc: freebsd-questions@FreeBSD. 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"; ?>| _______________________________________________ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.org"