user85748
user85748

Reputation: 1213

Best way to implement 'remembering' multiple values in a web app

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?

Upvotes: 0

Views: 224

Answers (7)

user85748
user85748

Reputation: 1213

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

Upvotes: 0

user85748
user85748

Reputation: 1213

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?

Upvotes: 0

DanSingerman
DanSingerman

Reputation: 36512

is there any other way to do it?

No. Certainly no good ways at least.

Upvotes: 0

V'rasana Oannes
V'rasana Oannes

Reputation: 644

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

Upvotes: 0

Gandalf
Gandalf

Reputation: 9855

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.

Upvotes: 1

Jarrett Widman
Jarrett Widman

Reputation: 6439

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.

Upvotes: 0

duffymo
duffymo

Reputation: 308938

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.

Upvotes: 0

Related Questions