One account can only log on to the same device at the same time

Source: Internet
Author: User
I use PHP to implement a single account and can only log on to the same device at the same time. Note that it is not the same IP address. Previously, I added a field in the MYSQL table to show whether I have logged on. if I have set logon to 1, exit from the field to 0. however, it was found that when the browser was forcibly closed, this field could not be set... I use PHP to implement a single account and can only log on to the same device at the same time. Note that it is not the same IP address.
Previously, I added a field in the MYSQL table to show whether I have logged on. if I have set logon to 1, exit from the field to 0.
However, it was found that this field cannot be set to 0 when the browser is forcibly closed!
After thinking for a long time, I didn't come up with a solution. later I saw on the Internet that I could use redis, so I started learning redis these two days. However, there is no way to learn this.
So I would like to ask, who has the experience to say how to implement it? Thank you! Happy Mid-Autumn Festival!

Reply content:

I use PHP to implement a single account and can only log on to the same device at the same time. Note that it is not the same IP address.
Previously, I added a field in the MYSQL table to show whether I have logged on. if I have set logon to 1, exit from the field to 0.
However, it was found that this field cannot be set to 0 when the browser is forcibly closed!
After thinking for a long time, I didn't come up with a solution. later I saw on the Internet that I could use redis, so I started learning redis these two days. However, there is no way to learn this.
So I would like to ask, who has the experience to say how to implement it? Thank you! Happy Mid-Autumn Festival!

For Redis, you can use the hash structure to store account login information.

Hash structure: key field value

Hash related use command http://redisdoc.com/hash/inde...

Specific implementation:

In the hash structure, historical data is overwritten when the same key field is used to write data.

Redis> hset key field TestRedis> hget key field"Test"Redis> hset key field RunRedis> hget key field"Run"

In this way, you can meet the requirements of a single account by specifying a key to store account login information. field is the primary key of each account, so each login will clear the last login information, the previous logon information becomes invalid, so that the previous logon status becomes invalid.

If you consider logon from different devices, you can change the field to devicename-uid to ensure that only one logon information exists for one device.

You need to know what you need?
Single sign-on or single device restriction
Is a single device a single computer with multiple browsers?

A solution for using mysql

If you do not consider efficiency, you only need to record your original data in mysqlLogged on?Is added next to the fieldExpiration TimeAndUnique identifier of a deviceThe two fields change the previous condition for determining whether to log on from "whether to 1" to "whether it is 1 and has not expired and the unique identifier of the device is consistent ". Updated every time a user has an operationExpiration TimeIf no operation is performed for a period of time, the logon status will automatically expire, in this way, you can solve the problem of "setting this field to 0 when you forcibly close the browser.

Simple implementation using phpredis

If you are newredisAnd only need to useredisYou do not know much about the data structure when controlling user logon,stringType can meet your requirements (if you can, usehashMay be better ).

The following describes the related classes provided by the phpredis extension as the background:

Assume thatUser IDLog on to an account of 100, and record the login device information to redis.


  Connect ($ redisHost, $ redisPort); $ cacheName = 'deviceuuid: user '. $ userId; $ deviceUUID = getDeviceUUID (); // assume that the getDeviceUUID () function is used to obtain/generate the unique identifier of the device. $ timeout = 600; // The user automatically goes offline after 10 minutes without any operation $ redis-> set ($ cacheName, $ deviceUUID); $ redis-> setTimeout ($ cacheName, $ timeout );}

Before the device performs other operations, it must update the expiration time of the device information in redis.


  Connect ($ redisHost, $ redisPort); $ cacheName = 'deviceuuid: user '. $ userId; $ deviceUUID = getDeviceUUID (); // assume that the getDeviceUUID () function is used to obtain/generate the unique identifier of the device. $ timeout = 600; // The user automatically goes offline after 10 minutes without any operation $ cachedDeviceUUID = $ redis-> get ($ cacheName); $ isTimeout = false ===$ cachedDeviceUUID; $ isTheRightDevice = $ deviceUUID ===$ cachedDeviceUUID; if ($ isTimeout |! $ IsTheRightDevice) {return false;} $ redis-> setTimeout ($ cacheName, $ timeout); return true ;}

When the user account in the device exits, you need to clear the device information in redis.


  Connect ($ redisHost, $ redisPort); $ cacheName = 'deviceuuid: user'. $ userId; $ redis-> delete ($ cacheName );}

Of course, the above solution using the string type instead of the hash type is not reasonable in terms of resource utilization and efficiency. If you want to have a better understanding and application of redis, read Redis in action. Specifically, when using redis in php, you can choose to use phpredis extension or predis.

A project created some time ago may have such a thing. the general goal is to have only one terminal to log on to this account, that is, not one account to log on at the same time.

The solution is to add a field token to the database. each login generates a new token based on the timestamp and continuously checks the token throughout the process. if the token changes, a user logs on elsewhere.

Add a field to the database: temporary token. after login, the temporary token will be generated randomly, and the user will generate the corresponding sesssion based on the token. after logging on to another device, the temporary token is updated. the session of the original device cannot match the database token!

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.