friar tux has asked for the wisdom of the Perl Monks concerning the following question:

hi, I am trying to make a perl script that handles voting, but there are a few things I simply don't know how to do. I have searched the internet for a way to count votes from a text script and display the names of the winners + how to make sure each person can only vote once but no one seems to cover what i am trying to find. maybe I am not searching for the right key words I am not sure. I made an attempt which is posted below my code is very amatuer I would appreciate any help.

<html> <form action=cgi-bin/vote.cgi method=post> <h1> Computer Club Voting system<hr></h1> enter your password:<input type=text name=pass> <input type=submit> </html>

vote.cgi

#!/usr/bin/perl use CGI ':standard'; print header; $name=param('name'); $pass=param('pass'); $i=param('i'); if ($pass == 777){ $i++; print "<form action=/cgi-bin/voter.cgi method=post>"; print "welcome member please select your name <select name=fname><opti +on>david</option><option>sarah</option><option>howard</option></selec +t><hr>"; print "place your vote"; print "for president <select value=$prez><option>sam</option><option>h +osea</option><option>howard</option></select><hr>"; print "for vice-president <select value=$vice><option>wendy</option><o +ption>ann</option><option>george</option></select><hr>"; print "for secretary treasurer <select value=$secretary><option>martha +</option><option>tom</option><option>david</option></select><hr>"; print " <a href=voter.cgi?prez=$prez&vice=$vice&secretary=$secretary&f +name=$fname>review your vote</a>"; if ($i ==1){ open (DATA, ">>data.txt"); print DATA "$fname | $prez | $vice | $secretary"; close DATA; } }else{ print "you are not a member"; } open (VOTE, ">>voted,txt");

voter.cgi

#!/usr/bin/perl use CGI ':standard'; print header; $fname=param('fname'); $prez=param('prez'); $vice=param('vice'); $secretary=param('secretary'); $i=param('i'); print " president = $prez<hr>"; print " vice-president = $vice<hr>"; print " secretary = $secretary<hr>"; if ($i ==1){ open (DATA, ">>data.txt"); print DATA "$fname , $prez , $vice , $secretary"; close DATA; } print " thank you for voting"; print "<a href=www.smccme.edu>main page</a>";

I have tried multiple combination of writing and reading from the files and carrying over the variables sometimes it will write the president but not the name of the voter and other times just the voters name will be displayed and written to the text file. there must be a simple way to make a script check for a name written to a text file and if it isn't there let them vote and write the name to the file so they can't vote again? as far as displaying the names of the winners.... I have no clue maybe some kind of array variable would help with the voting once aspect? anyway I am a noob so the simplest answer would be best and all help is appreciated

Replies are listed 'Best First'.
Re: simple voting script
by Anonymous Monk on Apr 27, 2011 at 21:34 UTC

    An important question is; what happens if two people hit submit at the same time? Do you have any locking scheme to prevent two instances from overwriting or corrupting the data files?

    What about using a database, such that you can make a single transaction out of "delete name from pending votes, and increment vote tally for candidate A, B, C". Then you don't need to record how they voted, you're protected from simultaneous access, and they can't lose their vote or vote twice.

      technicalities like two people hitting the submit button at the same time aren't my biggest concern. I just really want the functionality to work and I will confront any problems when I have them. my main problem is making a script that lets only members vote, vote only once and displays the current leaders\winners.

        "I just really want the functionality to work"

        You may wish to reconsider your definition of "technicalities" and of "to work" as -- to me -- a working script/package is one which does what's expected, pretty much regardless of "technicalities (aka "unexpected events") and without opening yawning security holes.

        Then you should probably review html standards (4.01 assumed, as it will be much easier to find help on that than on 5.) and read (in Tutorials) about CGI; about SQLite, and other basics for the voting operation you're trying to set up.

        This is a case where there are "simple" answers and "safe" answers... and -- IMO --the two sets have very few intersections. You may want to consider running this year's election on dead trees or by fone or... and postpone realization of your ambitions until you have enough information (knowledge) to accomplish them.

        my main problem is making a script that lets only members vote, vote only once
        Then the first problem you need to solve (and the problem is only partial solvable by coding) is "how do I recognize a user", and "how much I'm willing to spend on it". There are all kinds of solutions, from just asking who the user is (easily forged) to encrypted connections, one-time-only paths, RSA dongles, etc. Things to consider (but the list isn't exhaustive):
        • How anonymous must the voting process be? Tracking who voted gives up some of the anonymity.
        • Do you need to exclude man-in-the-middle attacks?
        • Do the voters themselves need to know they're talking to your voting system? That is, does authentication need to be both ways?
        • The more complicated it gets, the more people won't bother to vote. Where's the trade-off when balancing "security" vs "voter count"?

        If corrupting all your vote data is a mere "technicality", then you should just stick with paper voting.