How can PHP effectively prevent repeated voting?

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.