sIsMember('question:vote:' . $questionId, $userId);" and other methods to prohibit repeated voting. ."/> sIsMember('question:vote:' . $questionId, $userId);" and other methods to prohibit repeated voting. .">
Home >Backend Development >PHP Problem >How to prohibit repeated voting in php
How to prohibit repeated voting in php: first put the votes into the set; then pass "$redis->sIsMember('question:vote:' . $questionId, $userId);" etc. The method is to prohibit repeated voting.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
How to prohibit repeated voting in php?
Whether it is real voting or online voting, the uniqueness of the voter's identity must be ensured.
Generally we will put the votes into storage, but it is obviously too slow to query it in the database. Now with redis (http://redis.io), you can put it in It’s in the set.
Below I use php code to demonstrate this process. Other languages are similar
$userId = '111'; $questionId = '222'; // 用户111向问题222投票,那么我们把222的userId放到名为question:vote:111的set里 $redis->sAdd('question:vote:' . $questionId, $userId); // 判断222用户是否对111问题投过票了,只需判断111是否被包含在question:vote:111的set里 $isVoted = $redis->sIsMember('question:vote:' . $questionId, $userId); // 我们还可以取消投票,只需要从set里移除这个元素 $redis->sRem('question:vote:' . $questionId, $userId);
Everything is done in memory, very fast.
Recommended learning: " PHP video tutorial》
The above is the detailed content of How to prohibit repeated voting in php. For more information, please follow other related articles on the PHP Chinese website!