Home > php教程 > PHP开发 > How to use memcache to share session data in thinkPHP with multiple domain names

How to use memcache to share session data in thinkPHP with multiple domain names

高洛峰
Release: 2016-12-22 10:13:16
Original
1116 people have browsed it

The example in this article describes the implementation method of using memcache to share session data in thinkPHP with multiple domain names. Share it with everyone for your reference, as follows:

1. Origin of the problem

Slightly larger websites usually have several servers, each server runs modules with different functions and uses different second-level domain names, and one For a highly integrated website, the user system is unified, that is, a set of usernames and passwords can be used to log in to each module of the entire website. It is relatively easy for each server to share user data. You only need to put a database server on the back end, and each server can access user data through a unified interface. But there is still a problem, that is, after the user logs in to this server, when entering other modules of another server, he still needs to log in again. This is a one-time login, and all common problems are mapped to technology. In fact, it is between various servers. How to share SESSION data.

2. The working principle of PHP SESSION

Before solving the problem, let’s first understand the working principle of PHP SESSION. When the client (such as a browser) logs in to the website, the visited PHP page can use session_start() to open the SESSION, which will generate the client's unique identification SESSION ID (this ID can be obtained/set through the function session_id()). The SESSION ID can be retained on the client in two ways, so that when requesting different pages, the PHP program can learn the client's SESSION ID; one is to automatically add the SESSION ID to the GET URL, or the POST form, by default. Below, the variable name is PHPSESSID; the other is to save the SESSION ID in the COOKIE through COOKIE. By default, the name of this COOKIE is PHPSESSID. Here we mainly use the COOKIE method for explanation, because it is widely used.

So where is the SESSION data stored? On the server side of course, but instead of saving in memory, it's saved in a file or database. By default, the SESSION saving method set in php.ini is files (session.save_handler = files), that is, the SESSION data is saved by reading and writing files, and the directory where the SESSION file is saved is specified by session.save_path, and the file name starts with sess _ is the prefix, followed by SESSIONID, such as: sess_c72665af28a8b14c0fe11afe3b59b51b. The data in the file is the SESSION data after serialization. If the number of visits is large, there may be more SESSION files generated. In this case, you can set up a hierarchical directory to save SESSION files, which will improve the efficiency a lot. The setting method is: session.save_path="N;/save_path", N is hierarchical. level, save_path is the starting directory. When writing SESSION data, PHP will obtain the client's SESSION_ID, and then use this SESSION ID to find the corresponding SESSION file in the specified SESSION file storage directory. If it does not exist, create it, and finally serialize the data and write it to the file. . Reading SESSION data is a similar operation process. The read data needs to be deserialized and the corresponding SESSION variable is generated.

3. The main obstacles and solutions for multi-server sharing SESSION

By understanding the working principle of SESSION, we can find that by default, each server will generate SESSIONID for the same client, such as for the same user Browser, the SESSION ID generated by server A is 30de1e9de3192ba6ce2992d27a1b6a0a, while the SESSION ID generated by server B is c72665af28a8b14c0fe11afe3b59b51b. In addition, PHP's SESSION data are stored separately in the file system of this server.

After identifying the problem, you can start to solve it. If you want to share SESSION data, you must achieve two goals: One is that the SESSION ID generated by each server for the same client must be the same and can be passed through the same COOKIE, which means that each server must be able to read the same SESSION ID. The COOKIE named PHPSESSID; the other is the storage method/location of SESSION data that must be accessible to all servers. Simply put, multiple servers share the client's SESSION ID and must also share the server's SESSION data.

The realization of the first goal is actually very simple. You only need to specially set the domain of the COOKIE. By default, the domain of the COOKIE is the domain name/IP address of the current server. If the domain is different, each The COOKIE set by the server cannot be accessed by each other. For example, the server of www.aaa.com cannot read or write the COOKIE set by the server of www.bbb.com. The servers of the same website we are talking about here have their own particularity, that is, they belong to the same first-level domain. For example: tieba.xiaoyuan.com and www.xiaoyuan.com both belong to the domain .xiaoyuan.com, then we can Set the domain of the COOKIE to .xiaoyuan.com, so that tieba.xiaoyuan.com, www.xiaoyuan.com, etc. can access this COOKIE. The setting method in the PHP code is as follows:

<?php
ini_set(&#39;session.cookie_domain&#39;, &#39;.xiaoyuan.com&#39;);
?>
Copy after login

In this way, the purpose of each server sharing the same client SESSION ID is achieved.

The second goal can be achieved by using file sharing. There are two ways to solve it, one is to use data storage session, and the other is to try memcache. Use MEMCACHE to solve it here.

I use the thinkphp framework, which already supports memcache access to sessions. After setting up the memcache server, you only need to set the memcache IP and port in the configuration file, and then specify the COOKIE_DOMAIN parameter, and then you can operate normally. Operate in session mode, and you can now share sessions with multiple domain names

I hope this article will be helpful to everyone’s PHP program design based on the ThinkPHP framework.

For more related articles on how to use memcache to share session data under thinkPHP multiple domain names, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template