Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP script lifecycle

Status
Not open for further replies.

MasterKaos

Programmer
Jan 17, 2004
107
GB
Ok, I know this is probably more of an Apache thing, but I'd like to hear how it relates to PHP specifically.

When the server gets several requests for a particular PHP file, does each PHP script get executed from beginning to end before the next one gets served? Specifically - if there is a file operation involved, can I rest assured that that the file will be opened, written to and closed before the next script tries to open it? Or is there a risk that before the first script closes the file the next script is allready trying to open it and failing? This is assuming of course that the file in question is only accessed by one particular script.

If the answer to the above question is that numerous instances of a particular script could be executing at the same time, and there is a risk that a file may be in use - would it be suficient to put the fopen() in a loop, say let it try a hundred times then give up, hoping that sooner or later the file will be free?

Would using MySQL instead of a flat file solve this problem? As I understand MySQL automatically handles locking so if two users try to insert or update a table simulataneously then MySQL will put one in a queue and wait till the first one has unlocked the table. Is this correct? Does this work when the same user (the one doing a connect in PHP) is doing both the inserts?

I guess thats a lot of questions for one post but hopefully you can see that it all relates to one issue, and perhaps if this isn't the right place to find the answers someone can point me in the right direction?

Thanks!
 
If two users hit the same PHP script on the same server at the same time, the two instantiations of the script will run at the same time.

PHP does provide file locking through the use of the flock() function ( But one should be careful with flock() -- if a script locks a file and then terminates without unlocking it, it is possible that all other scripts will be permanently blocked. Also, flock() does not work on all operating systems. Make sure you read the user-provided comments on the flock() man page.

MySQL (nd all database servers that I know of) takes all incoming queries and throws them onto the end of a queue. Another part of the server's software then processes, in turn, each query at the beginning of the queue. This is independent of the user or process providing the query.


But whether you need to jump through these hoops really depends on what you want to do. If you're simply appending values to the end of a file, so long as you invoke only one write operation in each instantiation of the script, you should be okay.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top