October 27th, 2000, 06:05 AM
-
when do you guys advise to use flock? I have a script that writes into a .txt file Everytime a user comes on my page
This is the code i use to write: not much special 
$hits is an array wich includes the complete .txt file and is writen (changed) back into that .txt file
Should i use flock or not?
$fd = fopen($filename , "w");
for($b=0 ; $b<count($hits) ; $b++)
{
fwrite($fd, $hits[$b]);
}
fclose($fd);
thnx
------------------
Until next time, I'll Remain!!
Quote the Dokus:"NeverMore!"
October 27th, 2000, 08:05 AM
-
You should use a database, actually.
If you can't use a database, then flock could help some. The problem comes when two people come to your site at the same time and try to access your .txt file. Will the one request just wait while the other reads, or will it give an error? Also, if one request reads the file, makes the changes, then a second request read the file, makes the changes, then the first request writes, then second request writes...you're basically missing one of the requests, so you're information wouldn't be accurate.
---John Holmes...
October 27th, 2000, 09:38 AM
-
Ok i can see that problem 
but what if someone writes into the .txt file, and locks it, and someone else wants to write into the .txt file! will it give an error? or will it place him in a wait? and as soon as the .txt file is being unlocked the second person writes to the file?
how does that work with flock?
i have a high traffic site, and i don;t mind inaccurate stats. but i can;t afford to have errors 
thnx for the reply btw
------------------
Until next time, I'll Remain!!
Quote the Dokus:"NeverMore!"
October 27th, 2000, 03:37 PM
-
If you have a high traffic site, then you REALLY need to look into getting a database installed. Install MySQL, it's free and already integrated into PHP. It's easy to learn, also.
I read the manual page on flock() and it says to use flock() just before you write to a file. A comment on the page says that if the server encounters a locked file, it waits until it is unlocked to read it.
You'll have to play around with it to get it to work right. I seems like this would slow you page down considerably if everyone is waiting for the file to be unlocked from the previous user...
---John Holmes...