Home  >  Article  >  Backend Development  >  Let’s talk about four solutions for PHP session sharing

Let’s talk about four solutions for PHP session sharing

藏色散人
藏色散人forward
2022-11-30 16:32:247211browse

This article will introduce you to the knowledge related to PHP session. Let's start with why this session sharing solution appears. I hope it will be helpful to friends in need~

Understand first Why does this session sharing solution appear?

As the projects of Internet companies are built in microservices and distributed environments, a project may be deployed in several or even many server clusters. At this time, a Question:

When a user has a session, for example, a user logs in to a project. Generally, projects of large companies have Nginx for reverse proxy.

Here Let’s briefly list several reverse proxy strategies commonly used by Nginx:

Polling strategy, weight ratio strategy, ip_hash strategy, and customizable strategies,

In Nginx’s reverse proxy Under the proxy, the user's request is generally distributed to different servers. However, if the user's request is stored on the server A of the request, then the user's session ID is stored in a ConcurrentHashmap of the JVM on the server. Use sessionID as key.

But if a service module requested by the user at this time may need to be called to server B, when the user initiates the request, the user's session ID is not stored on server B at this time, so the user will be asked again Perform a login operation. It may also lead to a situation where the user originally wanted to complete an order operation, but logged in several times.

So the session sharing solution is particularly important in distributed environments and microservice systems. [Recommended learning: "PHP Video Tutorial"]

Solution 1: Nginx-based ip_hash load balancing

In fact, it is how much the requested ip address is to you Take the model from each available server, and then distribute your request to the corresponding server through Nginx's reverse proxy. (Here, the available servers will be put into an array. If the result obtained by taking the modulo is , the request will be assigned to the server with the subscript in the server array.)

Detailed implementation:

You need to make corresponding modifications in the Nginx.conf file, according to your own available server

upstream backend{
    ip_hash;
    server 192.168.128.1:8080 ;
    server 192.168.128.2:8080 ;
    server 192.168.128.3:8080 down;
    server 192.168.128.4:8080 down;
 
}
server {
    listen 8081;
    server_name test.csdn.net;
    root /home/system/test.csdn.net/test;
    location ^~ /Upload/upload {
    proxy_pass http://backend;
 
    }
 
}

The advantages and disadvantages of this implementation:

Let’s talk about four solutions for PHP session sharing

Solution 2: Session replication based on Tomcat

This solution is actually to copy the generated sessionID to all servers in the system when the user requests it, so as to ensure that when the user requests it, it will be copied from When server A may call a module on server B, it can also ensure that service B also has the user's session ID, so that the user will not be asked to log in again. That solves the problem.

How to implement session replication in specific code?

Let’s talk about four solutions for PHP session sharing

Advantages and disadvantages of using session replication:

Let’s talk about four solutions for PHP session sharing

Solution 3: Use Redis as a unified cache for cached sessions

This solution is actually to put the sessionID generated every time the user requests it on the Redis server. Then set an expiration time mechanism based on the characteristics of Redis, so as to ensure that users do not need to log in again during the session expiration time in Redis we set.

How to implement the code:

Let’s talk about four solutions for PHP session sharing

The advantages and disadvantages of using Redis to implement session sharing:

Let’s talk about four solutions for PHP session sharing

Solution 4: Combining cookies

In fact, you can also put the session in the cookie, because every time the user requests, his cookie will be put in the request, so this can ensure that every time the user When making a request, it can be guaranteed that the user will not log in twice in a distributed environment.

The above is the detailed content of Let’s talk about four solutions for PHP session sharing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.im. If there is any infringement, please contact admin@php.cn delete