views:

126

answers:

7

In poll sites, a user (not logged in) can vote on a question only once. How does the web app 'remember' that the user has already answered a particular question?

one way is to set a cookie for every question she answers. another way to save the question id in the session. we could also store the value in a db, along with the session id.

is there any other way to do it?

A: 

Depends on how long you need to remember.

The cookie solution won't work for clients who won't accept cookies.

Saving the answered question IDs in the session works fine as long as the session is alive, but you lose the memory when the session is invalidated or times out.

Saving the answered question IDs in a database is the most long-lived and reliable. I'd recommend that you not save the session ID, since it's meaningless. A timestamp showing when the question was answered, along with the answer, might be more pertinent.

duffymo
A: 

I'd recommend using a cookie because you can persist it on the users machine and unless the go to another machine, delete their cookie or switch browsers it will be there. Because you have no logins this is the easiest way to track the specific user. If you try to user session then their session info is going to expire and they could come back and vote again.

jarrett
cookies sounds like the easiest way to do it. but do I create a cookie for every question? If the user answers 50 questions, do I need to create 50 cookies?
Essentially the value of a cookie is just a string, so you can easily store a delimited list of values.
DanSingerman
+1  A: 

You can also attempt to do it by user IP. Meaning save the IP address that answered the question. Problem with using the session is the user can always clear their cache/cookies and then come right back to your site and vote again. They could spoof their IP address too, but that's at least slightly harder. A combination of both storing in the session and in your DB is the most foolproof (and simplest) way IMO.

Gandalf
Multiple users can use a single IP, or a single session can span multiple IPs, so in general IP addresses are pretty useless for this sort of thing.
DanSingerman
Agreed it's not 100% foolproof, but sessions are even less so.
Gandalf
A: 

Hi,

You could persist their answers on the server in an XML file (one per user/ip/?). You still need to have some sort of authentication or way to link a user to their existing file when they start a new session.

Hope this helps,

Bill

Bill Mueller
Isn't it the same as saving the answers in a DB? I prefer dealing with DBs than XMLs
A: 

is there any other way to do it?

No. Certainly no good ways at least.

DanSingerman
-1: Not helpful.
musicfreak
How is giving a correct direct answer not helpful? Crazy...
DanSingerman
+1, downvote removed, you are right, no 100% foolproof way of doing it.
karim79
@karim79 thanks
DanSingerman
A: 

If I use cookies, shd I save each question ID (and answer) in a separate cookie? that would mean lot of cookies, right? say if the user answers 50 or 100 questions, that would be lots of cookies? is this OK?

As above, essentially the value of a cookie is just a string, so you can easily store a delimited list of values representing the questions. (Oh, and it is good etiquette here to add clarifications to your question, not add an answer down here)
DanSingerman
Sorry, I didn't realize :-(delimited values should work well
A: 

I checked at polldaddy. I cleared the session and cookies, it still remembered my choice. Looks like they are using the IP address

They're not (it didn't remember my vote across two browsers from the same machine)
DanSingerman