문제

in my application a user can post his/her article that other users can response upon ans vote up and down also like stackoverflow has for posted question ans answers.

How can I restrict user so that he/she can't vote twice ?

도움이 되었습니까?

해결책

Two methods:

  • Client Side: Set into cookie (article IDs for last 5 or 10 votes ) and do not allow to vote again. This is easily hackable but allows you to implement without any database changes!
  • Server side: track each and every vote up and vote down in a table like <{userID}, {vote UP/DOWN}, {articleID}>

StackOverflow uses database to store all this data. They also lock your vote after some period so you cannot undo your action.

다른 팁

Just store user id in the voting table.
So, you can always check if particular user already voted.
article-id, user-id, vote time and vote value columns is enough

I think the best way to do that is to store all votes and user ids in database, and if it gets bigger you can remove old records from the database after one week/month/etc and restrict voting on items that are older than a week/month/etc, so no one will be able to vote twice on the same item.

You will need to use some kind of server side scripting to remember users by IP or some other unique data. You'll have to store the IP or other data and remember it. When the user tries to up/down vote again, you'll have to check against that to see if you should let them do so. You should let them do the opposite though, in case they want to change their vote. You can do this with PHP and MySQL (and lots of other things).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top