I’ve created a poll system, only logged in users may vote. I have a table where I store a foreign key to users and polls like this:
poll_voters
| ref_user_id | ref_poll_id |
When a user votes I check if the user id exists with the current poll in the table. If it does, the user has already voted.
I’m wondering if I should do this on every page load to know whether to display vote forms, or the result (in case the user already has voted). Another way would be to have a session that says if the user has voted or not. And when he log in I would need to do the check and set the session of course.
What do you think I should do?
It would be best to have a session, and check the voted status when the user logs in. There’s no need to check against the database on every page load, that would be inefficient (although it would still work if the load is light).
You’d also need to refresh the page (or element, using ajax) when the user votes.