What is the best way to store unique votes?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
jchmski
Forum Newbie
Posts: 2
Joined: Wed Jan 26, 2011 4:16 pm

What is the best way to store unique votes?

Post by jchmski »

I'm designing a voting system and was wondering what the best method is for storing unique votes.

I remember a couple years back I used a simple table that stored ip_address and a post_id to record what user had viewed what page so they could not record multiple views from one IP. This method worked, but I remember page loads starting to become laggy as the table got bigger and bigger and I would have to search the table to make sure the person had not previously viewed the page.

So my question is, is there a better method for storing uniqueness? Facebook can do it with their Like button and that's everywhere :?
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: What is the best way to store unique votes?

Post by mecha_godzilla »

Using IP addresses is probably the best way to go as there aren't really any other unique values that you can use. A quick way to identify IP addresses would be to split the dotted quads into four separate columns and validate based on the first one, then the second, etc.

You might also want to consider indexing each column as the highest possible number for each quad is 255, which will significantly increase the query's performance when you start getting 000s of votes. I now use the same approach when retrieving records based on date values, and my queries run in about a quarter of the time than they used to without indexes. Not that this isn't IPv6 safe of course :)

You could also go with a simpler approach - set a cookie on the user's system and check to see if it's already there before doing an IP search - most users don't normally clear out there cookies cache.

If you really want to know how Facebook does it, search for

"how does facebook like button work"

or

"how to cheat facebook like button"

although it looks like it's tied-in to the user accounts, so all Facebook would be doing is storing the initial 'like' and when it tried to do it again an error would be generated (yes, I don't use Facebook in case it isn't already obvious!)

HTH,

Dodg
Post Reply