From owner-freebsd-questions@FreeBSD.ORG Sun Feb 29 06:08:40 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 9640016A4CE for ; Sun, 29 Feb 2004 06:08:40 -0800 (PST) Received: from dyer.circlesquared.com (host217-45-219-83.in-addr.btopenworld.com [217.45.219.83]) by mx1.FreeBSD.org (Postfix) with ESMTP id 6A63F43D31 for ; Sun, 29 Feb 2004 06:08:35 -0800 (PST) (envelope-from peter@circlesquared.com) Received: from circlesquared.com (localhost.petanna.net [127.0.0.1]) i1TE8v1J037869; Sun, 29 Feb 2004 14:09:08 GMT (envelope-from peter@circlesquared.com) Message-ID: <4041F279.1040206@circlesquared.com> Date: Sun, 29 Feb 2004 14:08:57 +0000 From: Peter Risdon User-Agent: Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.5b) Gecko/20031102 X-Accept-Language: en-us, en MIME-Version: 1.0 To: fbsd_user@a1poweruser.com References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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 List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 29 Feb 2004 14:08:40 -0000 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"; ?>|