Home >Backend Development >PHP Tutorial >Let's talk about four solutions for PHP session sharing
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"]
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:
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?
Advantages and disadvantages of using session replication:
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:
The advantages and disadvantages of using Redis to implement session sharing:
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!