You are here: Re: [PHP] incrementing a number from a text file « PHP « IT news, forums, messages
Re: [PHP] incrementing a number from a text file

Posted by Richard Lynch on 10/10/40 11:10

> 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

 

Navigation:

[Reply to this 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

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