Re: Problem: Writing into Files?

From: Date: Sun, 02 Aug 2009 04:11:27 +0000
Subject: Re: Problem: Writing into Files?
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-296175@lists.php.net to get a copy of this message
Dear Ollisso, I tried it with FLock() but it still didn't work. This is what I did: [code] $fp = fopen($f, "r"); $count =fgets($fp, 1024); fclose($fp); $fw = fopen($f, "w"); while (!flock($fw, LOCK_EX)) sleep(1); $cnew = $count + 1; $countnew = fputs($fw, $count + 1); flock($fw, LOCK_UN); fclose($fw); [/code] Am I doing anything wrong here? Thanks! -- --- Contact info: Skype: parham-d MSN: fire_lizard16 at hotmail dot com email: parham90 at GMail dot com "Ollisso" <ollisso@fromru.com> wrote in message news:op.uxy29woc48vuyn@ol-n.kyla.fi... > On Sat, 01 Aug 2009 08:20:23 +0300, "Parham Doustdar" <parham90@gmail.com> > wrote: > >> Hi there, >> I've written a counter for my blog, which keeps the count of visitors in >> a file. However, when the visitors get too many, it resets to zero. Why? >> >> Here's the piece of code: >> >> [code] >> $f = $dir . '/view_counter' .EXT; >> $fp = fopen($f, "r"); >> $count =fgets($fp, 1024); >> fclose($fp); >> $fw = fopen($f, "w"); >> $cnew = $count + 1; >> $countnew = fputs($fw, $count + 1); >> return $cnew; >> [/code] >> >> I'm thinking this is caused by two visitors visiting the page at the >> same time; but is there a way to fix it, perhaps the reading/writing >> parameter? >> >> Thanks! >> > > Check: > http://www.php.net/flock > > > -- > Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

« previous php.general (#296175) next »