homepage Welcome to WebmasterWorld Guest from 54.196.63.93
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PHP voting mechanism using text files
no db allowed, is flock() the way forward?
Nick_W

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



 
Msg#: 427 posted 1:47 pm on May 22, 2002 (gmt 0)

'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

 

sugarkane

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 427 posted 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:

[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.

Nick_W

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



 
Msg#: 427 posted 4:09 pm on May 22, 2002 (gmt 0)

Great, thnks SugerKane, just the confirmation I needed.

Nick

Nick_W

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



 
Msg#: 427 posted 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:

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

lorax

WebmasterWorld Administrator lorax us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 427 posted 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.

Nick_W

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



 
Msg#: 427 posted 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.

Nick

lorax

WebmasterWorld Administrator lorax us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 427 posted 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.

Nick_W

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



 
Msg#: 427 posted 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?

Nick

lorax

WebmasterWorld Administrator lorax us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 427 posted 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:

[phpbuilder.com...]

I promise to let this alone now.:)
Gregg

Nick_W

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



 
Msg#: 427 posted 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.

Nick

kapow

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 427 posted 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.

Nick_W

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



 
Msg#: 427 posted 7:05 pm on May 30, 2002 (gmt 0)

Check your sticky kapow

Nick

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved