Search code examples
phpmysqlvoting

Reddit-style voting system, how do I avoid duplicate votes?


I have a table that stores a post. Each post has an id, title, content and a score. Currently, you can like a post and its score will increment and decrement, if you dislike it.

Now the thing I don't understand: how do I avoid that a user will vote more than once? Surely they can just refresh and vote again. I've read some articles that store cookies, etc. but can't you just disable cookies or clear them and vote again?

I was thinking you would have to store who has voted, or rather, the ID of who has voted too. However, I can't seem to visualize how I would go about this? Would I store the ID of the voter in the post they're voting, or something else?


Solution

  • You will need an extra table posts_vote or something. Put the fields user_id and post_id in it. If a user votes a post up, you insert both IDs in this table. If a user votes down, find the record and delete it.