|
PHP to implement multiple server sharing session data
|
|
First, the origin of the problem
Slightly larger sites, there are usually several servers, each server running different functions of the module, the use of different level two domain name, and a strong overall site, the user system is unified, that is, a set of user name, password in the entire site of the various modules can be logged in use. Each server sharing user data is relatively easy to implement, only need to put a database server on the back end, each server through the unified interface to user data access can be. But there is a problem, that is, the user after the server login, access to another server module, still need to log in, this is a login, all the problems, mapping to technology, in fact, is how each server to share the session data problem. Second, the PHP session working principle Before solving the problem, let's take a look at how the PHP session works. When a client (such as a browser) logs on to a Web site, the accessed PHP page can use Session_Start () to open the session, which produces the client's unique identifier (this ID can be obtained/set through the function session_id ()). The session ID can be kept on the client in two ways, so that when a different page is requested, the PHP program can learn the client's session ID, either by automatically adding the session ID to the URL of Get, or by the POST's form, by default, the variable is named P Hpsessid; the other is to save the session ID in a cookie by using a cookie, which by default is named Phpsessid. Here we mainly in the COOKIE way to explain, because the application is more extensive. Three, multi-server sharing session of the main obstacles and solutions By understanding the working principle of the session, we can find that by default, each server will have its own sessions ID for the same client separately, for example, for the same user browser, a server generates an ID that is 30DE1E9DE3192BA6CE2992D27A1B6A0A, while the B server generates C72665AF28A8B14C0FE11AFE3B59B51B. In addition, the PHP session data are stored separately in the file system of the server. As shown in the following illustration: Once you have identified the problem, you can proceed to solve it. To share session data, you have to implement two goals: one is that each server must have the same session ID for the same client, and it can be passed through the same COOKIE, which means that each server must be able to read the same one named Phpsessid COOKIE, and the other is how the session data is stored/positioned to ensure that each server is accessible. simply put, the session ID of a multiple-server shared client, and the server-side session data must also be shared. The implementation of the first goal is actually very simple, only need to make a special setting for the domain of the cookie, by default, the domain of the cookie is the domain name/IP address of the current server, and if the domain is different, the cookies set by each server are not accessible to each other, such as www.aaa.com servers are cookies that cannot read and write to www.bbb.com server settings. Here we call the same Web server has its particularity, that is, they belong to the same level of domain, such as: Aaa.infor96.com and www.infor96.com belong to the domain. infor96.com, then we can set the domain of the COOKIE as. Infor96.com, so aaa.infor96.com, www.infor96.com, and so on can access this COOKIE. The Setup method in the PHP code is as follows:
The purpose of sharing the same client session ID for each server is achieved. The implementation of the second goal can use file sharing methods, such as NFS, but it is somewhat complex to set up and operate. We can refer to the previous approach of the unified user system, that is, using a database to save session data, so that each server can easily access the same data source, get the same session data. The solution is shown in the following illustration: Four, code implementation First create the data table, the MySQL SQL statement is as follows:
Sesskey is session id,expiry for session expiration, and data is used to save session data. By default, session data is saved as a file, and you want to save it using the database, you must redefine the processing functions for each operation of the session. PHP provides the Session_set_save_handle () function, you can use this function to customize the session process, of course, first of all to change the Session.save_handler to user, you can set in PHP:
Next, let's focus on the Session_set_save_handle () function, which has six parameters: Session_set_save_handler (string Open, string close, string read, string write, string destroy, String gc) Each parameter is the function name of each operation, which is open, close, read, write, destroy, Garbage collected. There are detailed examples in the PHP manual where we use OO to implement these operations, and the detailed code is as follows: |