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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PHP

Creating a Fraud-proof Voting System
By: Ian Felton
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 117
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek