Home > Database > Redis > body text

Method to prohibit multi-terminal login based on Redis unordered collection

Release: 2020-05-28 09:21:23
forward
2275 people have browsed it

Method to prohibit multi-terminal login based on Redis unordered collection

Application Background

Assuming multiple application names are named A and B, users are prohibited from logging in from A and B at the same time. A login kicks B, and B login kicks A

Implementation idea

Set two unordered sets a_set, b_set

a b When logging in, execute

$redis->sAdd('a_set',$user_id);//A登录
$redis->sRem('b_set',$user_id);//踢B
Copy after login
$redis->sAdd('b_set',$user_id);//B登录
$redis->sRem('a_set',$user_id);//踢A
Copy after login

Before api obtains data, determine whether the id of the end is online (AB two The APIs of each end are separate)

A judgment:

if($redis->sIsmember('a_set',$user_id)){
   //true 
}else{
    //false
}
Copy after login

B judgment

if($redis->sIsmember('b_set',$user_id)){
    //true
}else{
    //false
}
Copy after login

Method used:

sadd key_set value 设置值到set中
sismember key_set value 判断值时候存在key_set里面
srem key_set value 移除指定值
smembers key_set 获取所有的value
Copy after login

For more redis knowledge, please pay attention to the redis introductory tutorial column.

The above is the detailed content of Method to prohibit multi-terminal login based on Redis unordered collection. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.im
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!