Phil
Phil

Reputation: 11175

disable multiple voting on user comments

Description:

I am creating comment/reply functionality to a web app that I built. I have a post that I want to link these comments too. To decrease spam and encourage community involvement I want to implement a voting system on each comment/reply.

Problem:

I know how to set up the database and I know how to show upvotes/downvotes. The only thing I don't know what to do is to keep the vote... voted even if the user refreshes the page. I don't want a user to be able to vote up more than once on a single post. Something like the voting on this site, it tracks that you have already voted with a yellow upvote.

What I have thought of:

In the future there may be 1000s of votes cast by a single user.

edit:

I figured out that storing the results in a database is a must. How can I check for every reply/comment if the user has valid voting privileges without making 1000s of calls?

Resources:

Helped with some further spamming problems, but didn't answer initial question: https://stackoverflow.com/a/2333085/185672

Old solution that explains how to count votes but not keep the "upvote" checked. http://www.9lessons.info/2009/08/vote-with-jquery-ajax-and-php.html

Bonus question:

If you guys know of a great script that can allow me to sync up a commenting system ( with votes ) into my already built app?

Also, I tried to find duplicates, but I just can't.

Upvotes: 2

Views: 649

Answers (2)

enhzflep
enhzflep

Reputation: 13099

Yeah, just keep a separate table to track user votes. Since you know which user is requesting the page, you can easily join the votes table to determine the current user's eligibility to vote on each post on the page. For each post, if they're eligible, output one version of html, if they're not then output another.

Once the ajax request asks for the php file that does the vote, you can then check once more that that user is eligible to vote - I.e they're changing their vote, or they haven't voted before.

Take yahoo's news stories for example - when you request a page that you've made a comment on, your own comment has disabled voting buttons. With some hacking of the page inside the browser's dev tools, you can enable the buttons. You can even click on them and vote for your own post - though only once.

So, you can see that they got 2/3rds of it right, and output html based on the user's eligibility to vote. They also prevent multiple voting (server-side), they just don't do a server-side check to ensure you're not voting for your own comment.

Stack Overflow on the other hand, always shows the same html - but when you click to vote for your own comment, the server-side code baulks at the idea and the response is basically 'bugger-off! you can't do that' having received a negative result from the server, the javascript on the page pops up the message, rather than updating the vote count.

Upvotes: 2

Sidharth Mudgal
Sidharth Mudgal

Reputation: 4264

I suggest you create a "hidden field" in your page that stores information about the upvote/downvote by the user. This is how you would use it:

If the user upvotes/downvotes, the javsvscript on the on the shows the upvote, sets the field to maybe true to indicate the vote status and would also send an AJAX request to the server where the server would record the vote status in the user profile database.

Then whenever the post is re-loaded for a particular user, the server sets the hidden field server-side to true or false, depending on the vote record that is stored in the database. The JS would, on load, check the hidden field and set the vote on the page accordingly (you might need an extra hidden field to indicate whether it is an up or down vote).

Upvotes: 0

Related Questions