mardi 20 janvier 2015

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: This is the code that checks if the ip is already in the database or not.



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

//public functions
public function vote($optionId)
{
$ip = $_SERVER['REMOTE_ADDR'];
if(!$this->_alreadyVote($ip)){
$sql ='INSERT INTO '.$this->_voterTable.' (id,option_id,ip) '.' VALUES(NULL,"'.mysql_real_escape_string($optionId).'","'.mysql_real_escape_string($ip).'")';

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


But now I want people to be able to vote each day with their IP address.


This is what I tried with my code so far:



private function _alreadyVote($ip)
{
$sql =
'SELECT * FROM '.$this->_voterTable.' WHERE ip="'.$ip.'" AND today= "'.$today'"';

$result = $this->_query($sql);
return sizeof($result)>0;
}

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

if(!$this->_alreadyVote($ip)){
$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).'")';

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


Aucun commentaire:

Enregistrer un commentaire