Multi-domain thinkphp sharing session data with Memcache mode (RPM)

Source: Internet
Author: User
Tags php session

First, the origin of the problem

Slightly larger sites, usually have several servers, each server running a different function of the module, the use of different two-level domain names, and a strong overall site, the user system is unified, that is, a set of user names, passwords 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 access user data. However, there is still a problem, that is, after the user login to another server after the other module, still need to log back in, this is a login, all the problems, mapping to the technology, in fact, how each server to achieve sharing SESSION data problem.

Ii. how the PHP SESSION works

Before you solve the problem, let's 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 results in a client's unique ID session ID (this ID can be obtained/set through the function session_id). Session ID can be retained in two ways in the client, so that when requesting a different page, the PHP program can learn the client's session ID, one is to automatically add the session ID to the Get URL, or post form, by default, the variable name is PHP Sessid; the other is to save the SESSION ID in a cookie by using a cookie, by default, the name of the cookie is PHPSESSID. Here we mainly explain in the cookie way, because the application is more extensive.

So where does the SESSION data be stored? It is, of course, on the server side, but not in memory, but in a file or database. By default, the session set in PHP.ini is saved by files (Session.save_handler = files), that is, the session data is saved using a read-write file, and the directory saved by the session file is SESSION.SAVE_PATH specifies that the file name is prefixed with Sess_ followed by SESSIONID, such as: Sess_c72665af28a8b14c0fe11afe

3b59b51b. The data in the file is the session data after the serialization. If the access volume is large, may produce the session file will be more, then you can set the hierarchical directory to save the session file, the efficiency will be improved a lot, the setting method is: Session.save_path= "N;/save_path", N is a graded series, save _path is the starting directory. When the session data is written, PHP will obtain the client's session_id, and then according to the session ID to the specified session file to save the directory to find the corresponding session file, does not exist to create, and finally to serialize the data to write to the file. Reading session data is a similar process, and the data that is read needs to be deserialized to generate the corresponding session variables.

Three, multi-server sharing SESSION of the main obstacles and solutions

By understanding how the SESSION works, we can find that, by default, each server generates SESSIONID for the same client individually, such as the session ID generated by a server for the same user browser Is 30de1e9de3192ba6ce2992d27a1b6a0a, while the B server generates C72665AF28A8B14C0FE11AFE3B59B51B. In addition, the session data of PHP is stored separately in the file system of this server.

Once you have identified the problem, you can proceed with the solution. To share session data, two goals must be achieved: one is that each server must have the same session ID as the same client, and it can be passed through the same COOKIE, which means that each server must be able to read the same name as PHPSESSID. A COOKIE; the other is how the SESSION data is stored/placed to ensure that each server has access to it. In short, the session ID of the multi-server shared client, and the server-side session data must also be shared.

The first goal of the implementation is very simple, only need to set the domain of the cookie is special, by default, the domain of the cookie is the domain name/IP address of the current server, and the domain is different, the individual servers set cookies are not accessible to each other, such as Www.aaa.com server is not able to read and write www.bbb.com server settings cookies. Here we say that the server of the same site has its particularity, that is, they belong to the same domain, such as: Tieba.xiaoyuan.com and www.xiaoyuan.com belong to the domain. xiaoyuan.com, then we can set cookies The domain is. xiaoyuan.com so that tieba.xiaoyuan.com, www.xiaoyuan.com, and so on can access this cookie. The Setup method in the PHP code is as follows:

<?php
Ini_set (' Session.cookie_domain ', '. xiaoyuan.com ');
?>
The purpose of each server sharing the same client SESSION ID is achieved.

The second goal of the implementation can use the file sharing method, there are 2 ways to solve, one is to use the data inventory session, there is a trial memcache. Here to solve with memcache.

I am using the thinkphp framework, has supported the memcache way to access the session. After a good memcache server, just set the Memcache IP and port in the configuration file, and then specify the Cookie_domain parameters. Then you can follow the normal operation of the session, this time can be multi-domain sharing session

Multi-domain thinkphp sharing session data with Memcache mode (RPM)

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.