Welcome to WebmasterWorld Guest from 54.225.31.78

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP voting mechanism using text files

no db allowed, is flock() the way forward?

     
1:47 pm on May 22, 2002 (gmt 0)

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member



'ello!

I've been asked to put together a survey for a client with no db access. Clearly text files are the way forward but what if 2 people try to vote at once?

Is this a problem, and if so, is flock() the way forward?

I read the manual entry on it but am none the wiser? (well, not much anyway)

Many thanks

Nick

2:27 pm on May 22, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> 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:

[webmasterworld.com...]
[webmasterworld.com...]

The basic syntax for flocking in PHP is:

[perl]
$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
[/perl]

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)

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Great, thnks SugerKane, just the confirmation I needed.

Nick

8:21 pm on May 22, 2002 (gmt 0)

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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:

ababbbbaabbbddccdcdabab etc

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!

Nick

12:28 pm on May 24, 2002 (gmt 0)

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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)

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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.

Nick

4:13 pm on May 24, 2002 (gmt 0)

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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)

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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?

Nick

4:37 pm on May 24, 2002 (gmt 0)

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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:

[phpbuilder.com...]

I promise to let this alone now.:)
Gregg

8:24 am on May 25, 2002 (gmt 0)

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member



No, it's covered. Appends of less than a blocksize are atomic therefor no locking is neccessary.

Nick

6:36 pm on May 30, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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)

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Check your sticky kapow

Nick

 

Featured Threads

Hot Threads This Week

Hot Threads This Month