| 2:27 pm on May 22, 2002 (gmt 0)|
> Is this a problem, and if so, is flock() the way forward?
Yep, simultaneous users writing to a file can cause nasty problems. Flock (File Lock) will prevent that if used correctly - see these threads for more info:
The basic syntax for flocking in PHP is:
$pointer=fopen ( $filename, "w+"); # open your file for reading and writing
flock ($pointer, "LOCK_EX"); # lock your file
# do stuff to the file here
flock ($pointer, "LOCK_UN"); # unlock your file
fclose($pointer); # close the file
Note: it may not actually be necessary to unlock the file before closing it - Perl does this automatically, but I'm not sure about PHP.
| 4:09 pm on May 22, 2002 (gmt 0)|
Great, thnks SugerKane, just the confirmation I needed.
| 8:21 pm on May 22, 2002 (gmt 0)|
Just an update,
I spoke to Rasmus Lerdorf, or rather he spoke to me ;) and a bunch of others on the php mailing list about this and came up with a much simpler way of doing it:
If the 'survey' has 4 possible answers: a, b, c and d then all you need do is append either of the above letters to the end of the file that records the results.
You end up with a file like this:
The size of the file is the total amount of votes, and substr_count() will return the number of times a letter occors to give you the individual votes!
Neat huh? No locking, no messing, no worrying about simultaneus read/writes, brilliant!
| 12:28 pm on May 24, 2002 (gmt 0)|
|all you need do is append either of the above letters to the end of the file that records the results. |
But how does this solve the original problem?
Granted, you won't need to open the file to get the total number of votes. But you must still open the file to read the individual votes or to cast a vote.
| 2:26 pm on May 24, 2002 (gmt 0)|
Sure, but you needn't lock it. Which is the point.
Even updating it needs no locking as it matters not whether the chars get put in in some kind of order or not. Only that they go in.
| 4:13 pm on May 24, 2002 (gmt 0)|
Correct, you don't have to :) lock the file.
Although, if you don't you run the risk of two simultaneous writes occurring which could cause troubles. I misunderstood and thought you were trying to avoid this possibility.
| 4:20 pm on May 24, 2002 (gmt 0)|
No simulaneous writes don't matter, they only mater if the file contains an integer (for example) that's value is vital. In this case if two users put a 'b' in the file... who cares?
| 4:37 pm on May 24, 2002 (gmt 0)|
Um..I can't tell if we're on the same page or not Nick. :)
So at the risk of repeating what you already know/have considered..I'm worried about file corruption and loss of data. If two users both cast a vote and the server spins two threads accessing the same file at the same time you could be in for a surpize. Here's a thread on the subject:
I promise to let this alone now.:)
| 8:24 am on May 25, 2002 (gmt 0)|
No, it's covered. Appends of less than a blocksize are atomic therefor no locking is neccessary.
| 6:36 pm on May 30, 2002 (gmt 0)|
I've been wondering how this is done for ages. Thankyou Nick_W
Any chance of someone posting the code for this ;)
I'm rather new to PHP so my coding is not very good.
| 7:05 pm on May 30, 2002 (gmt 0)|
Check your sticky kapow