Now we need to use the voting system on a website. We need to prevent repeated voting. What is the more effective method. Because the voting status needs to be remembered continuously, the session or cookie must be unreliable. Do you have to write files or databases? By the way, what does Segmentfault use? Now we need to use a voting system on a website. We need to prevent repeated voting. What is a more effective method.
Because the voting status needs to be remembered continuously, the session or cookie must be unreliable. Do you have to write files or databases?
By the way, what method does Segmentfault use to store the voting status?
Reply content:
Now we need to use the voting system on a website. We need to prevent repeated voting. What is the more effective method.
Because the voting status needs to be remembered continuously, the session or cookie must be unreliable. Do you have to write files or databases?
By the way, what method does Segmentfault use to store the voting status?
Both real-world voting and online voting must ensure the uniqueness of the voter's identity. Generally we will put the vote into the storage, but the database query is obviously too slow, and now with redis (http://redis.io), you can put it in the set. The following code uses php to demonstrate this process.
$ UserId = '000000'; $ questionId = '000000'; // If user 111 votes to question 222, put the userId of 111 to the name question: vote: $ redis-> sAdd ('Question: vote: 'In set 111 :'. $ questionId, $ userId); // determines whether a 222 user has voted for the 111 issue. You only need to determine whether 111 is included in question: vote: $ isVoted = $ redis-> sIsMember ('Question: vote: 'In the 111 set :'. $ questionId, $ userId); // We can also cancel the vote, just remove this element from the set $ redis-> sRem ('Question: vote :'. $ questionId, $ userId );
Everything is done in the memory, very fast
I remember writing the IP address and corresponding options into the database when I did it.