Problem: Writing into Files?

Problem: Writing into Files?

am 01.08.2009 07:20:23 von Parham Doustdar

------=_NextPart_000_027D_01CA128D.7D580690
Content-Type: text/plain;
charset="windows-1256"
Content-Transfer-Encoding: quoted-printable

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 =3D $dir . '/view_counter' .EXT;
$fp =3D fopen($f, "r");
$count =3Dfgets($fp, 1024);
fclose($fp);
$fw =3D fopen($f, "w");
$cnew =3D $count + 1;
$countnew =3D 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!

--=20
---
Contact info:
Skype: parham-d
MSN: fire_lizard16 at hotmail dot com
email: parham90 at GMail dot com
------=_NextPart_000_027D_01CA128D.7D580690--

Re: Problem: Writing into Files?

am 01.08.2009 09:33:22 von Ollisso

On Sat, 01 Aug 2009 08:20:23 +0300, "Parham Doustdar"
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/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Problem: Writing into Files?

am 02.08.2009 06:11:27 von Parham Doustdar

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" wrote in message
news:op.uxy29woc48vuyn@ol-n.kyla.fi...
> On Sat, 01 Aug 2009 08:20:23 +0300, "Parham Doustdar"
> 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/



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: Problem: Writing into Files?

am 02.08.2009 11:21:05 von Richard Heyes

Hi,

> ...

You can write a single byte to the file to "increment" the counter,
then to read the count just use filesize(). I believe the a fopen()
mode will handle locking for you. It will result in a slowly growing
file, but space isn't exactly at a premium nowadays, and when the file
gets to a certain size (eg 1 gazillion k) you could use a summary
file.

--
Richard Heyes
HTML5 graphing: RGraph - www.rgraph.net (updated 25th July)
Lots of PHP and Javascript code - http://www.phpguru.org

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Problem: Writing into Files?

am 02.08.2009 11:44:03 von Ollisso

On Sun, 02 Aug 2009 07:11:27 +0300, "Parham Doustdar"
wrote:

> 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!
Hello,

Actually you should do something like this (according to manual)

$f=fopen("file", "r+");
while(!flock($f, LOCK_EX)) sleep(1);
$count = fgets($f,1024);
ftruncate($f, 0);
fwrite($f, $count+1);
fclose($f);

Problem in your case: first time you open file for reading, but you don't
flock it, so it can read non-writen file.




--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Problem: Writing into Files?

am 02.08.2009 12:14:42 von Parham Doustdar

Now this one always sets the file to one for some reason. I'll reiterate
just in case there's been a misunderstanding on my part:
[code]
$fp = fopen($f, "r+");
while (!flock($fp, LOCK_EX))
sleep(1);
$count = fgets($fp,filesize($fp));
ftruncate($fp, 0);
fwrite($fp, ($count+1));
flock($fp, LOCK_UN);
fclose($fp);
Thanks a lot again for your help.
--
---
Contact info:
Skype: parham-d
MSN: fire_lizard16 at hotmail dot com
email: parham90 at GMail dot com
"Ollisso" wrote in message
news:op.ux03zprl48vuyn@ol-n.kyla.fi...
> On Sun, 02 Aug 2009 07:11:27 +0300, "Parham Doustdar"
> wrote:
>
>> 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!
> Hello,
>
> Actually you should do something like this (according to manual)
>
> $f=fopen("file", "r+");
> while(!flock($f, LOCK_EX)) sleep(1);
> $count = fgets($f,1024);
> ftruncate($f, 0);
> fwrite($f, $count+1);
> fclose($f);
>
> Problem in your case: first time you open file for reading, but you don't
> flock it, so it can read non-writen file.
>
>
>
>
> --
> Using Opera's revolutionary e-mail client: http://www.opera.com/mail/



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: Problem: Writing into Files?

am 02.08.2009 12:16:25 von Parham Doustdar

Dear Richard,
I don't quite know how I can write a bite into a file. I also looked into a
manual and couldn't find a mention of FLock-ing in the explaination for
FOpen parameters.
Thanks a lot for your help.

--
---
Contact info:
Skype: parham-d
MSN: fire_lizard16 at hotmail dot com
email: parham90 at GMail dot com
"Richard Heyes" wrote in message
news:af8726440908020221r22e1efb3g321ba4140bfa42d6@mail.gmail .com...
> Hi,
>
>> ...
>
> You can write a single byte to the file to "increment" the counter,
> then to read the count just use filesize(). I believe the a fopen()
> mode will handle locking for you. It will result in a slowly growing
> file, but space isn't exactly at a premium nowadays, and when the file
> gets to a certain size (eg 1 gazillion k) you could use a summary
> file.
>
> --
> Richard Heyes
> HTML5 graphing: RGraph - www.rgraph.net (updated 25th July)
> Lots of PHP and Javascript code - http://www.phpguru.org



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: Problem: Writing into Files?

am 02.08.2009 22:31:14 von Richard Heyes

> I don't quite know how I can write a bite into a file. I also looked into a
> manual and couldn't find a mention of FLock-ing in the explaination for
> FOpen parameters.

Ok, from memory:

fwrite(fopen('/tmp/counter', 'a'), '1');
?>

The 1 could be any single byte character I guess.

--
Richard Heyes
HTML5 graphing: RGraph - www.rgraph.net (updated 25th July)
Lots of PHP and Javascript code - http://www.phpguru.org

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Problem: Writing into Files?

am 03.08.2009 10:34:56 von Ollisso

On Sun, 02 Aug 2009 13:14:42 +0300, "Parham Doustdar"
wrote:

> Now this one always sets the file to one for some reason. I'll reiterate
> just in case there's been a misunderstanding on my part:
> [code]
> $fp = fopen($f, "r+");
> while (!flock($fp, LOCK_EX))
> sleep(1);
> $count = fgets($fp,filesize($fp));
> ftruncate($fp, 0);
> fwrite($fp, ($count+1));
> flock($fp, LOCK_UN);
> fclose($fp);
> Thanks a lot again for your help.


This will work:

$f = 'file.txt';
$fp = fopen($f, "r+");
while (!flock($fp, LOCK_EX))
sleep(1);
$count = intval(fread($fp,1024));
fseek($fp,0,SEEK_SET);
ftruncate($fp, 0);
fwrite($fp, ($count+1));
fclose($fp);

echo $count;

(first you should create file file.txt manually, only then it will work)

--

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: Problem: Writing into Files?

am 03.08.2009 12:05:52 von Richard Heyes

Hi

> The thing with this method is that it's just like the previous one; it opens
> then adds something. I'm assuming if again two visitors visit at the same
> time, this'd reset to zero.

No, the "a" mode (IIRC) handles file locking for you. Even if it
doesn't, the file won't be truncated, so you won't lose your count.

--
Richard Heyes
HTML5 graphing: RGraph - www.rgraph.net (updated 25th July)
Lots of PHP and Javascript code - http://www.phpguru.org

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php