How to log in only one user in php

藏色散人
Release: 2023-03-17 16:36:01
Original
5469 people have browsed it

php method to achieve only one user login: 1. Initialize socket in the uni-app front end; 2. Receive a "force exit" type message in real time; 3. Use the PHP backend to receive "device unique" "Identification" parameter; 4. Send the message according to the clientid in the cache.

How to log in only one user in php

The operating environment of this tutorial: Windows 10 system, PHP version 8.1, Dell G3 computer.

How to log in only one user in php?

uni-app combines PHP to achieve single user login

Single user Login, that is, in an application, the same user can only log in once online. If a user logs in, he will be pushed offline immediately on other devices. After confirmation, clear the login loading on the device and return to the login interface.

uni-app is currently able to package Android, IOS, WeChat applet, Toutiao Alipay applet and H5 by using the vue.js framework and only needs to write a set of code. By using the HBuilder tool, it is convenient for debugging and cloud packaging. , Regarding Apple certificates, we recommend CW.PUB, https://cw.pub/index/document/index. Use HBuilder to create a jailbreak package and then sign it on that website to install it on a normal Apple phone. However, there are other methods on the Internet that I will not list here.

Generally, APP will use a third-party message push platform for single user login, although uni-app can also connect to push platforms such as Umeng and Jiguang. But still due to time, the docking platform review and other processes do not allow time. I used gatewayworkman and websocket for instant chat before, so single-user login is also implemented using websocket.

uni-app socket single-user login example

1. The uni-app front end sends the unique identifier of the current device when initializing the socket, and then receives a "force exit" in real time ” type of message is just a simple example.

//初始化 socket.on('init', () => { //连接初始化 socket.send({ type: 'login', token: uni.getStorageSync('access_token'), device_no: plus.device.uuid,//手机设备唯一编号 }); }).on('quit_push',(res)=> { if(res) { uni.showModal({ title: '退出通知', content: '你的账号在其他设备上登录!', showCancel: true, cancelText: '取消', confirmText: '确定', success: res => { if(res.confirm) { uni.clearStorageSync() store.commit('chat/clear') uni.reLaunch({ url:"../../pages/login/index" }) }else if(res.cancel) { uni.clearStorageSync() store.commit('chat/clear') uni.reLaunch({ url:"../../pages/login/index" }) } } }); } });
Copy after login

2. The backend receives the "device unique identification" parameter and first checks whether the cache exists. There is no clientid that records the device identification and socket.

3. The login interface receives the device identification, and the identification record retrieved from the cache or library is judged to see if it is consistent with the currently received device identification. If not, the message is sent based on the clientid in the cache.

$is_online = Db::name('UserLoginClient')->where('user_id',$user['id'])->order('id desc')->find(); if(isset($device_no) && $device_no && $is_online['device_no'] != $device_no && !empty($is_online['device_no'])) { Tools::sendToClient($is_online['client_id'],json_encode([ 'type' => 'quit_push', 'data' => 'ip', 'message' => '强制下线' ])); }
Copy after login

4. Tool class sendToClient method part

public static function sendToClient($client_id, $message) { Gateway::sendToClient($client_id, $message); }
Copy after login

Push single user login example

1. First connect the friend The alliance, including front-end and back-end, has added SDK and used their methods.

2. Message push has a unique value "token", here referred to as "pushtoken", which is generated by the client and can identify a unique device.

3. When the backend logs in, it receives the pushtoken and also determines whether the pushtoken exists. If it does not exist, it will be stored with the user ID as the key.

4. When it exists, it will be judged whether it is consistent with the cache. If it is consistent, the cache time will be lengthened. If it is inconsistent, a message will be pushed to the old pushtoken (in the cache) and the new pushtoken will be cached.

if (self::$headToken && Cache::has(self::$prefix . self::$userId)) { if (self::$headToken == Cache::get(self::$prefix . self::$userId)) { Cache::set(self::$prefix . self::$userId, self::$headToken, self::$timeOut); } else { // 换了手机,客户端重新发送pushtoken到服务端,服务端与缓存中的pushtoken比较,不同则给原来pushtoken手机推送一条并重新缓存新的token // modify by wensen on 20180816 // $addr = getCity(); $addr = getMobCity(); $ip = request()->ip(); if ($addr) { $addr['province'] = empty($addr['province']) ? '' : $addr['province']; $addr['city'] = empty($addr['city']) ? '' : $addr['city']; // $address = "\t" . $addr['country'] . "-" . $addr['region'] . "-" . $addr['city'] . " (IP:" . $ip . ")\t"; $address = "\t" . $addr['country'] . "-" . $addr['province'] . "-" . $addr['city'] . " (IP:" . $ip . ")\t"; } else { $address = "IP:" . $ip . ""; } $OldToken = Cache::get(self::$prefix . self::$userId); if (strlen($OldToken) == 64) { $content = array( 'title' => 'APP紧急通知', 'body' => '您的账号于:' . date('Y-m-d H:i:s') . '在' . $address . '处登录,若不为您本人登录,请您立即修改密码!', 'pull_service' => 'login' ); \umeng\Push::send($OldToken, 'unicast', $content, 'message', true); } elseif (strlen($OldToken) == 44) { $content = array( 'pull_service' => 'login', 'msg' => '您的账号于:' . date('Y-m-d H:i:s') . '在' . $address . '处登录,若不为您本人登录,请您立即修改密码!' ); \umeng\Push::send($OldToken, 'unicast', $content, 'message', true); } Cache::set(self::$prefix . self::$userId, self::$headToken, self::$timeOut); } } else { Cache::set(self::$prefix . self::$userId, self::$headToken, self::$timeOut); }
Copy after login

5. The APP client receives the push and performs pop-up prompts and exit processing.

6. The above is the push method encapsulated according to Umeng's SDK, including unicast, broadcast, jumping to application activities, jumping to web page connections, etc.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to log in only one user 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
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!