Redis implementation session sharing, to solve an account can only be landed in a terminal

Source: Internet
Author: User
Tags redis
An account in a computer landing, at this time in B computer Landing, will be a kick off the line, need to solve two problems:
First, to ensure that the account can only be landed in one place;
Second, send a message after landing notice;

For the first problem, we can use the session stored in the Redis, the implementation session in multiple sites, multiple servers sharing the case, unified through the session_id to manage the user's session data,

The following is a session of code stored in Redis and shows an example of getting session data through session_id to Redis.

 $redis _config = [' Host ' => ' 192.168.200.229 ', ' Port ' => 6379]; @ini_set ("Session.save_handler",
"Redis");

@ini_set ("Session.save_path", ' tcp://'. $redis _config[' host ']. '. $redis _config[' Port ');
Session_Start ();
$_session[' user_id '] = 123;
$_session[' user_name '] = ' Helin ';

$_session[' email ' = ' xxxxxx@163.com ';
$session _id = session_id ();
echo $session _id;

Ssr242gfioap1auk0njtdg07q4 echo "<br>";
$redis = new Redis ();
$redis->connect ($redis _config[' host '), $redis _config[' Port ');
Redis uses session_id as key and stores the Echo $redis->get As String (' Phpredis_session: '. $session _id);

User_id|i:123;user_name|s:5: "Helin"; email|s:14: "Xxxxxx@163.com";
echo "<br>";
echo Json_encode ($_session); {"user_id": 123, "user_name": "Helin", "email": "xxxxxx@163.com"} exit; 


with the implementation of the above code, we can consider the logic of the implementation:
Step one: When the user logs in, save the session_id to the user datasheet, and then generate a key based on the UID. Save some user information in Redis (generate a login ID and session_id, etc.)
Step two: When another terminal logs in with the same account, get the session_id of the user's table. And do the operation of step one;
Step three: The session_id obtained through step two becomes the old information, it may be currently in use, may also be expired, at this time can be based on this session_id from the Redis to clear out the data inside, whether it is not valid;

After the above processing, if there is a landing in another terminal, then the rest of the session should be cleared away, and then access to the landing page must be the exit state; So the first question is solved, now to the second question:

If the real-time requirements are not high, and do not need to consider the pressure of large data, such as an ERP management system, the company's internal use, the landing of the people will not be too much, you can use AJAX timed request polling way to achieve, this will be much simpler, The implementation of the form is through the server to determine whether the current session data is still there, if it does not exist, according to the previous storage in the Redis of the landing state data and user information, to find out that the user table in the session_id and Redis inside the session_id is consistent , if it is logged in and inconsistent, you can prompt the user to have landed at another terminal. If it is session_id consistent, it should certainly be expired. At this point, you can clear off the user-related Redis data and do a redirect login.
Real-time requirements, or performance considerations, you can use the socket way, and Ajax is not the same as the way it can push data to the client, the user experience is the best, controllability is strong, is to achieve a little more complex than the first way, Can be achieved by means of Socket.io + node.js.

In addition to the above, we can also change the way of thinking, do the simplest processing, when the user in another terminal landing, you can immediately exit, do not make a hint, the current if you need to do other operations, and then to judge the landing problem is also possible, but not so real time, There is also the beginning of planning to consider the process of how the user experience will be better.

Related Article

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.