PHP
  Home arrow PHP arrow Page 3 - Creating a Fraud-proof Voting System
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Creating a Fraud-proof Voting System
By: Ian Felton
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 102
    2004-12-21

    Table of Contents:
  • Creating a Fraud-proof Voting System
  • One Person, Multiple Addresses, Still One Vote
  • One Vote.php, Two States
  • Trust but Verify

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Creating a Fraud-proof Voting System - One Vote.php, Two States


    (Page 3 of 4 )

    Next, we’ll examine vote.php. Vote.php has two states: one when a user clicks on a link to vote for a song and one when they have submitted an email address and posted the form. In the first state, Vote.php has one parameter passed to it: the band’s id. The first chunk of code checks that the band’s ID is passed to the script. If it is, the script goes to the database and grabs the name of the band and the name of the song.

    //Check if the band’s ID is sent as a parameter
    if (isset($_GET["profile"])) {
    //Select the database
    mysql_select_db($database, $db);
    //escape parameters to avoid tampering
    $ID = (get_magic_quotes_gpc()) ? $_GET['profile'] : addslashes($_GET['profile']);

    //select the band’s name and song name from the database
    $query_tracks = sprintf("SELECT t1.track, t2.band FROM songs as t1, bands as t2 WHERE t1.CCID = %s and t2.CCID=%s", $ID, $ID);
    $tracks = @mysql_query($query_tracks, $db);
    $row_tracks = @mysql_fetch_assoc($tracks);
    }

    The second state of the script is more involved. When the form is posted, it checks for two conditions: that the same address hasn’t been used to vote for that song that day and that the same computer hasn’t been used to vote for that song that day. If both test pass, then an email is sent to the address for the user to click for verification.

    //Check that the form has been posted.
    if ((isset($_POST["insert"])) && ($_POST["insert"] == "form1")) {
    //select the database
    mysql_select_db($database, $db);
    //get today’s month and day
    $date = date("md");
    //initialize variables that were posted from the form
    $email = (get_magic_quotes_gpc()) ? $_POST['email'] : addslashes($_POST['email']);
    $song = (get_magic_quotes_gpc()) ? $_POST['song'] : addslashes($_POST['song']);
    $ID = (get_magic_quotes_gpc()) ? $_POST['bandID'] : addslashes($_POST['bandID']);
    //Setup data for test #1. Has the same email been used today to vote for this song?
    $query_Votes = sprintf("SELECT time FROM votes WHERE email = %s AND bandID = %s ORDER BY time DESC", GetSQLValueString($email, "text"),
    GetSQLValueString($ID, "int"));
    $Votes = @mysql_query($query_Votes, $db);
    $row_Votes = @mysql_fetch_array($Votes);
    $current_time = $row_Votes['time'];
    $month2 = substr($current_time, 4, 2);
    $day2 = substr($current_time, 6, 2);
    $date2 = $month2.$day2;
    //Setup data for test#2. Has the same computer been used today to vote for this song?
    $query_Votes = sprintf("SELECT time FROM votes WHERE IP = %s AND bandID = %s ORDER BY time DESC", GetSQLValueString($_SERVER['REMOTE_ADDR'], "text"), GetSQLValueString($ID, "int"));
    $Votes = @mysql_query($query_Votes, $db);
    $row_Votes = @mysql_fetch_array($Votes);
    $current_time = $row_Votes['time'];
    $month3 = substr($current_time, 4, 2);
    $day3 = substr($current_time, 6, 2);
    $date3 = $month3.$day3;

    //Does the vote pass both test? If so, insert the vote into the database.
    if ( ($date > $date2) && ($date > $date3) ) {
    $insertSQL = sprintf("INSERT INTO votes (email, pending, song, bandID, IP) VALUES (%s, %s, %s, %s, %s)",
    GetSQLValueString($email, "text"),
    GetSQLValueString('0', "int"),
    GetSQLValueString($song, "text"),
    GetSQLValueString($ID, "int"), GetSQLValueString(strip_tags($_SERVER['REMOTE_ADDR']), "text"));
    $Result1 = mysql_query($insertSQL, $db);
    //retrieve the ID of the vote
    $id = mysql_insert_id();
    //Get the time it was entered
    $selectSQL = sprintf("SELECT time FROM votes where id = %s", GetSQLValueString($id, "int"));
    $Result2 = mysql_query($selectSQL, $db);
    $row_Time = @mysql_fetch_array($Result2);

    //Send an email for use for verification using the timestamp as a key
    mailPending($_POST['email'], $_POST['bandID'], $row_Time['time']);
    $'message = "Thanks for your vote. Come back tomorrow, vote again and keep listening to your favorite bands on United Bands.";
    }
    //If the vote doesn’t pass the test notify the user that they have to wait until tomorrow.
    else {
    $message = "You have to wait until tomorrow to vote again for this song. You can vote for other songs you haven't voted for today.";
    }
    }
    //Mails the voter a verification email.
    function mailPending($to, $bandID, $time) {
    $message .='Thanks for voting. Please click this link to verify your vote. http://unitedbands.com/verify.php?band='.$bandID.'&time='.$time.'&email='.$to;
    $subject = "Thanks for Voting at UnitedBands.com";
    $mailResult=send_email($to, $subject, $message);
    }

    More PHP Articles
    More By Ian Felton


       · You describe as most important requirement of the example that users shall not be...
       · Yup, that's pretty much what they are doing- not allowing folks from the same IP...
       · ... a NOT foolproof way of checking votes, and the only thing unusual from the...
       · "NOTHING is foolproof to a sufficiently talented fool!"
       · I agree. Using a user's IP address as a means of verifying the user, is a classic...
       · "There are a few ways to get around this system. ‘Fraud-proof’ is used in the...
       · That Jeb Bush is governor of Florida does not imply the 2000, or 2004 election was...
       · Thank you.
       · Yes of course, Florida was on the up and up. Katherine Harris, the state's chief...
       · 1 ip 1 vote is an unacceptable requirement for most websites. Sure, it will help...
       · You must surely be right to call this man a moron...In fact, Diebold's CEO DIDN'T...
       · Well, a Democrat, Chad Staton, linked to the NAACP attempted to register George...
       · Creating ballots - no. Removing large numbers of (mainly Democrat) ballots - yes....
       · A governor does not have the power to do that. A governor signs his state's...
       · How come there is no response? Talk about loooooozah! Democrats are whiny pathetic...
       · Peyton, you are really a moron. Good there are people like you to belive on...
       · Let's call each other names now, shall we?I am still interested in how Governor...
       · Can't we all just get along?
       · I second that.
       · http://www.gregpalast.com/detail.cfm?artid=27&row=2The powers in Florida simply...
       · [url=http://archives.cnn.com/2002/ALLPOLITICS/05/28/justice.florida.voting]Read this...
       · What does this have to do with the article?
       · At one time WebTV gave all users one of a handful of IP addresses.Poor WebTV...
       · I've always thought it was funny that just because Bush won Fl and his brother...
       · You should make this voting system as a download...like a zip or something. It...
     

       

    PHP ARTICLES

    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT