Reply to RE: [PHP] incrementing a number from a text file

Your name:

Reply:


Posted by Richard Lynch on 10/04/20 11:10

> Is it possible to let the OS handle the collisions?
>
> For example:
>
> $last_value = system('cat counter.txt', $retval);
> $last_value++;
> $result = system('echo $last_value > counter.txt', $retval);
>
> ...just a suggestion!

Even if your OS guarantees locks on > re-direct, you'd still have the
*WRONG* number in there any time two people did this at the same time.

>
> Regards
> /Luis
>
>
>
> -----Original Message-----
> From: Richard Lynch [mailto:ceo@l-i-e.com]
> Sent: Thursday, March 10, 2005 3:14 PM
> To: Ross Hulford
> Cc: php-general@lists.php.net
> Subject: Re: [PHP] incrementing a number from a text file
>
>> I want to read a number from an external (txt) file and increment
>> it.then save the number back on the text file.
>> I know this is possible but want a simple amd economical way to do
> this.
>
> That's what you *THINK* you want to do :-)
>
> But what happens when *TWO* users hit that same script at exactly the
> same time.
>
> At best, you get:
>
> User1 User2
> Reads 9 Reads 9
> Writes 10 Writes 10
>
> and when you should have 11, you've only got 10.
>
> [aside] "My amp goes to 11!" [/aside]
>
> But it's worse than that: It's entirely poassible that your script,
> instead of having two users write "10" one after the other, will have
> two users both trying to write "10" at EXACTLY the same time.
>
> The result is comparable to a head-on collision between two trains.
> Your file is TOAST.
>
> This is why so many early "hit counter" scripts back in the day were
> always getting messed up and reset to 0.
>
> What you really want, almost for sure, is an SQL database with
> "sequences"
>
> You have to forge those in MySQL by making a table with auto_increment.
>
> You *can* use http://php.net/flock, but even that is a Bad Idea, because
> flock under Un*x is self-imposed -- If some *other* program/script/user
> decides not change that file and doesn't use flock, well, they're not
> STOPPED from doing that.
>
> flock is therefore all too subject to human fallibility when you
> re-work, re-write, or add more code to your system.
>
> The SQL guys have worked out this problem, in all respects.
>
> This is one of those things that SEEMS so simple that turns out to be a
> hell of a lot more involved than you thought at first. So it goes.
>
> --
> Like Music?
> http://l-i-e.com/artists.htm
>
> --
> PHP General Mailing List (http://www.php.net/) To unsubscribe, visit:
> http://www.php.net/unsub.php
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


--
Like Music?
http://l-i-e.com/artists.htm

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация