Search code examples
phpmysqldatabasewebvoting

Daily voting system with IP


Hello everyone!

I know using IP addresses isn't the best way to go for a voting system but in my case it's fine!

What I want to achieve: An admin can set a question for the poll. Users answer to the question and can vote for each answer daily using their IP address.

What I have so far: An admin can set a question for the poll. Users can answer to the question using their IP address once. (you can only vote once for each IP adres)

Database: Table options: id and name This is the table that holds the answers to the poll. Table voters: id, option_id, ip, today and number This is the table that holds the voter ip address, answer he wants to vote for, the date when he voted(not yet working) and the number that is used for getting the total of the votes.

I have 3 php files: muziek.php, insertM.php and vote.php

muziek.php shows the question and answer of the poll.

insertM.php lets users insert an answer to the poll.

vote.php does the voting for the poll. People can only use their ip adres once, now I want to make it so people can use their IP adres daily.

vote.php:

private function _alreadyVote($ip,$today)
        {
            $sql = "SELECT * FROM ".$this->_voterTable." WHERE ip='".$ip."' AND DATE(today) = CURDATE()";
            $result = $this->_query($sql);
            return sizeof($result)>0;
        }

        //public functions
        public function vote($optionId)
        {           
            $ip  = $_SERVER['REMOTE_ADDR'];
            $today = date('Y-m-d');

                if(!$this->_alreadyVote($ip,$today)){

                $sql ='INSERT INTO '.$this->_voterTable.' (id,option_id,ip,today) '.' VALUES(NULL,"'.mysql_real_escape_string($optionId).'","'.mysql_real_escape_string($ip).'","'.mysql_real_escape_string($today).'")';
mysqli_query($conn, $sql);

                $result = mysql_query($sql,$this->_con);
                if(!$result){
                    die('unable to insert'. mysql_error());
                }
            }        
        }

I need to track the day and check if the current date equals the date in the database of the voting user. If it is not the same the user should be able to vote again and a new date will be set.

Thanks for reading! Please ask me if you want me to post more code.


Solution

  • Perform this kind of query to determine wether the combination of the IP-adress and the current date returns any results:

    $sql = "SELECT COUNT(*) as rowCount FROM ".$this->_voterTable." WHERE ip='".$ip."' AND DATE(latestVoteDate) = CURDATE()";
    

    If this returns rows (or a rowcount) - the 'persons' ability to vote should be denied. If this is not the case, voting can be made possible. After the vote, update the latestVoteDate of the row with the ip adress ($ip) to CURDATE().

    $conn = mysqli_connect("localhost","my_user","my_password","my_db");
    $query = mysqli_query( $conn,  $sql );
    $data = mysqli_fetch_assoc( $query );
    
    if($data['rowCount'] > 0)
    {
       die("You are not allowed to vote");
    }
    else
    { 
       // Show the voting page or partial
    }
    

    Then, when the vote action actually happened, update the row which contains the right IP to the current date:

    $sql = "UPDATE ".$this->_voterTable." SET latestVoteDate = CURDATE() WHERE ip = '" . $ip . "';";
    mysqli_query($conn, $sql);