An implementation method of sharing session data using Memcache mode in the case of thinkphp multiple domain names _php instance

Source: Internet
Author: User
Tags session id php session php template smarty template

This article illustrates the implementation method of sharing session data using Memcache mode in the case of thinkphp multiple domain names. Share to everyone for your reference, specific as follows:

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 you solve the problem, start by understanding 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 requesting a different page, 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 PHP Sessid; 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.

So where is the session data stored? Of course it's on the server side, but not in memory, but in a file or database. By default, the session saved in php.ini is files (Session.save_handler = files), which saves session data in the form of a read-write file, while the session file holds a directory that is saved by the SESSION.SAVE_PATH specifies that the filename is prefixed by sess_ followed by SESSIONID, such as: sess_c72665af28a8b14c0fe11afe3b59b51b. The data in the file is the session data after serialization. If the visit is large, may produce the session file will be more, at this time can set the hierarchical directory to save the session file, the efficiency will be improved a lot, set the method is: session.save_path= "N;/save_path", N for graded series, save _path is the start directory. When the session data is written, PHP obtains the session_id of the client, and then finds the corresponding session file in the directory of the specified session file, which is not present, and then the data is serialized and then written to the file. Reading session data is similar to the operation process, to read out the data needs to be serialized, generate the corresponding session variables.

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

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

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: Tieba.xiaoyuan.com and www.xiaoyuan.com belong to the domain. xiaoyuan.com, then we can set cookies 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 sharing the same client session ID for each server is achieved.

The realization of the second goal can use file sharing way, there are 2 ways to solve, one is to use data inventory session, there is a trial memcache. This is solved with memcache.

I'm using the thinkphp framework, and I've supported memcache access session. After the 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 multiple domain sharing session

More interested in thinkphp related content readers can view the site topics: "thinkphp Introductory Course", "thinkphp Template Operation Skills Summary", "thinkphp Common Methods Summary", "Smarty Template Introductory Course" and "PHP template technology Summary."

I hope this article will help you with the PHP program design based on thinkphp framework.

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.