Suprah Posted May 6, 2011 Share Posted May 6, 2011 Hey. I just needed a little help with a voting system for articles I've put together in PHP. Essentially what happens is someone clicks the "Vote up" button which takes them to the "voteup.php" page. This is the "voteup.php" page where it updates the database for that given article and increments the "votes" column by one. This works perfectly. But the problem is people can do this infinite times and give articles infinite votes. I need it so that each IP address can only vote on each specific article once. Any ideas on how this could be done? Voteup.php <?php include("connect.php"); $id2 = $_GET['id']; // Get the ID of the article that will be voted up $con = mysql_connect("localhost","lconditn_admin","hello"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("lconditn_database", $con); mysql_query("UPDATE base SET votes = votes+1 // Add one vote to the article WHERE id2 = '$id2'"); mysql_close($con); ?> Thanks a lot for any help with this. Quote Link to comment https://forums.phpfreaks.com/topic/235710-vote-system-stop-multiple-votes/ Share on other sites More sharing options...
rbrown Posted May 6, 2011 Share Posted May 6, 2011 I would do it based on thier "user id" otherwise if they could disconnect from the internet and get another ip or spoof their ip or if you did it with cookies they could move off the site and destroy the cookie and come back to your site and keep voting. I guess it depends on how critical you want to be on enforcing the voting... If you are allowing them to just vote then you will need to do it by ip. Either way I would do it where you have a table indexed by "ip" or "user id" and a field for "votes" then as they vote it would append the "article id" to to the "vote" field using a delimiter some thing like this: 1^4^22^200^3000 etc... then when they go to vote, lookup their "ip" or "user id" and grab the "votes" and explode it based on your delimiter then use in_array to check if they already voted or not on the article. $voting = explode('^', $votes); if (in_array($article_id, $voting)) { Code to allow them to vote and add the "article id" to the "vote" field $votes .= $votes.'^'.$article_id; update database with new vote } else { echo "sorry you all ready voted on this article!" } either way... depending on the traffic for your site... the database table could get huge. So you would need to keep an eye on it. Or if that could be a problem, then have "user id" or "ip" "article id" and date then dump the data after x amount of days or months or years. Or you could trim the votes after x amount of votes so say after voting for 100 articles it would dump the first one listed then add to the end. Lots of ways to skin the cat... Bob Quote Link to comment https://forums.phpfreaks.com/topic/235710-vote-system-stop-multiple-votes/#findComment-1211531 Share on other sites More sharing options...
rbrown Posted May 6, 2011 Share Posted May 6, 2011 oops I flipped the "if" statement... or add a ! in front of in_array and drop the . in front of the = sorry... Wouldn't let me reedit... Quote Link to comment https://forums.phpfreaks.com/topic/235710-vote-system-stop-multiple-votes/#findComment-1211534 Share on other sites More sharing options...
Psycho Posted May 6, 2011 Share Posted May 6, 2011 By IP? That means if there are multiple users behind the same NAT router you will only allow one vote for all of them. Not to mention, people can spoof their IP address. Anyway, since you are allowing votes for different articles for the same users, I think you should use a separate table to capture the votes. An example would be a table called "votes" with field for "base_id", "IP", "vote". Since you only appear to let people vote in the affirmative (i.e. vote up but not down) the "vote" field is not really necessary since you could just do a COUNT() of the votes for a particular article. But, I'd use it anyway in case you need more functionality later on. So, on your "voteup.php" page you would change the query as follows: $IP = $_SERVER['REMOTE_ADDR']; $query = "INSERT INTO votes (`base_id`, `IP`, `vote`) VALUES ($id2, '$IP', 1)"; mysql_query($query); To get the votes for an article, you would need to do a join on the votes table. (By the way, you would need to remove the votes field from the "base" table). Example query SELECT base.*, SUM(votes.vote) as votes FROM base JOIN votes ON base.id = votes.base_id GROUP BY base.id Quote Link to comment https://forums.phpfreaks.com/topic/235710-vote-system-stop-multiple-votes/#findComment-1211540 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.