How to prohibit repeated voting in php

藏色散人
Release: 2023-03-11 17:30:01
Original
1759 people have browsed it

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.

How to prohibit repeated voting in php

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);
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template