PHP Voting
- Fallin-Again
-
Fallin-Again
- Member since: Aug. 24, 2004
- Offline.
-
- Forum Stats
- Member
- Level 11
- Blank Slate
I have a script for my site to vote on videos, but it only has one problem that i cant figure out. When someone votes it stores thier ip, and the date that they voted in the database to stop them from voting more than once per day. When they vote the next day, the script tells them that they arleady voted that day before it clears the database, and they have to click it a second time for it to work.
<?php
//connect to the database
include('db_config.php');
//grab this users IP address
$ip = $_SERVER["REMOTE_ADDR"];
//grab the video ID
$vId = mysql_real_escape_string($_POST['id']);
//query the database
$vQuery = mysql_query("SELECT * FROM `videos` WHERE `id` = '$vId' LIMIT 1")or die("Error: ".mysql_error());
$vA = mysql_fetch_array($vQuery);
//check that the date in the database is the correct date. if not then clear the database and change the date
$oldDate = $vA['voteday'];
$todayDate = date('dmY');
if($oldDate != $todayDate){
$upDate = mysql_query("UPDATE `videos` SET `users` = '', `voteday` = '$todayDate' WHERE `videos`.`id` = '$vId';") or die("Error1: ".mysql_error());
if($upDate){$cont = 'go';}
}else{
$cont = 'go';
}
if($cont == 'go'){
//grab the proper information from the database
$whoVote = explode(",", $vA['users']);
if(in_array($ip,$whoVote)){
echo('You have already voted on this submission today.');
exit();
}
$newVote = $vA['votes'] + 1;
$newUsers = $vA['users'].','.$ip;
$upRow = mysql_query("UPDATE `videos` SET `votes` = '$newVote', `users` = '$newUsers' WHERE `videos`.`id` = '$vId';") or die("Error2: ".mysql_error());
//we want to track total voted in a day so lets add to the vote table
$vTable = mysql_query("SELECT * FROM `votes` WHERE `date` = '$todayDate' LIMIT 1") or die("Error1: ".mysql_error());
//if there isnt a row where the date matches up lets create one
$vRowCount = mysql_num_rows($vTable);
if($vRowCount == 0){
$msql_insert = mysql_query("INSERT INTO `votes` (`date`, `votes`) VALUES ('$todayDate','1')") or die(mysql_error());
}else{
$vGrab = mysql_fetch_array($vTable);
$newNum = $vGrab['votes'] + 1;
$upVRow = mysql_query("UPDATE `votes` SET `votes` = '$newNum' WHERE `votes`.`date` = '$todayDate';") or die("Error2: ".mysql_error());
}
echo("Thanks for voting!");
}
?> yea yea yea yea yea yea yea yea yea yea yea yea yea yea yea yea yea yea yea yea yea yea yea yea
- NinoGrounds
-
NinoGrounds
- Member since: Nov. 28, 2005
- Offline.
-
- Forum Stats
- Member
- Level 19
- Programmer
That's realy a lot of code to go through, but I think I can help you with the logistics, because I was making the same thing recently for my site (see the nsfw link in my sig). I allow one IP per vote. IP range is just too big to add time value when user voted. It's not necessary, and it makes the programming part a lot easier.
So, this is how I do it:
click on a like or dislike. i store them in different columns. and then I append the IP to the IP_voted_history with a "|". So my IP history looks thus. "ip|ip|ip". And then I just use explode and in_array() to determine if that IP had previously voted.
- fyko-chan
-
fyko-chan
- Member since: Aug. 18, 2008
- Offline.
-
- Forum Stats
- Member
- Level 10
- Programmer
$vA = mysql_fetch_array($vQuery);
//check that the date in the database is the correct date. if not then clear the database and change the date
$oldDate = $vA['voteday'];
$todayDate = date('dmY');
if($oldDate != $todayDate){
$upDate = mysql_query("UPDATE `videos` SET `users` = '', `voteday` = '$todayDate' WHERE `videos`.`id` = '$vId';") or die("Error1: ".mysql_error());
if($upDate){$cont = 'go';}
<strong>$vA = mysql_fetch_array($vQuery);</strong>
}else{
$cont = 'go';
} - fyko-chan
-
fyko-chan
- Member since: Aug. 18, 2008
- Offline.
-
- Forum Stats
- Member
- Level 10
- Programmer
Bold tags inside code tags didn't work apparently, but that should work!
- Fallin-Again
-
Fallin-Again
- Member since: Aug. 24, 2004
- Offline.
-
- Forum Stats
- Member
- Level 11
- Blank Slate
yea yea yea yea yea yea yea yea yea yea yea yea yea yea yea yea yea yea yea yea yea yea yea yea
- Momo-the-Monkey
-
Momo-the-Monkey
- Member since: Oct. 15, 2005
- Offline.
-
- Forum Stats
- Member
- Level 45
- Musician
I haven't run a speed test, but creating an array from the database query, and then sifting through that array seems a bit slower than just searching the database for the IP address.
Don't get me wrong, I'm not saying this is the best solution, just the one I would use.
I would create the votes database with columns "ID, VID_ID, IP, DATE, VOTE" or something like that. Then when someone votes, I put in their information along with the date (ID is just an incremental ID for all that "correct" database structure and what not). On the vote page, I would check to see if their IP with today's date was in the database. If not, add it and vote, else deny voting.
You can then either just leave the data in the database, clear it out every month or so, or clear it out every voting day using cronjobs or something (which then might let you eliminate the need for a "DATE" column).
Again, I haven't speed-tested these methods, so I don't know which one is faster. Just an opinion :)
- Fallin-Again
-
Fallin-Again
- Member since: Aug. 24, 2004
- Offline.
-
- Forum Stats
- Member
- Level 11
- Blank Slate
I have only done one cronjob before.......gotta try'er out.
yea yea yea yea yea yea yea yea yea yea yea yea yea yea yea yea yea yea yea yea yea yea yea yea
- Momo-the-Monkey
-
Momo-the-Monkey
- Member since: Oct. 15, 2005
- Offline.
-
- Forum Stats
- Member
- Level 45
- Musician
At 11/6/11 11:23 AM, Fallin-Again wrote: I have only done one cronjob before.......gotta try'er out.
Not too hard if your host allows it.
Check here. A simple tutorial about it. There are plenty more on google.
I also just noticed that after you clear out the date from the database, you don't recall the mysql_fetch_array() function. Meaning that $whoVote is an array of users from the first time you called mysql_fetch_array()....aka $va['users'] is the users BEFORE you reset the database.
That's why you have to refresh the page to get it to work.


