Zend Framework for implementing multi-server sharing session data _php example

Source: Internet
Author: User
Tags session id php session zend zend framework

In this paper, the Zend Framework is used to implement multiple server sharing session data. Share to everyone for your reference, specific as follows:

First, the origin of the problem

Large Web sites typically have multiple servers and use multiple level two domain names. Such a server-generated session cannot be shared by all servers. This allows users to log in one place and not pass all

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 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.

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 Save method set in php.ini is files (Session.save_handler = files), which saves session data in a read-write file, while the session file is saved in a directory by SESSION.S AVE_PATH specifies that the filename is prefixed with sess_, followed by the session ID, 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 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:

<?php
ini_set (' Session.cookie_domain ', '. infor96.com ');
? >

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 put the data in the memcache. So that each server can easily access the same data source, get the same session data.

The solution is shown in the following illustration:

Ok.

More interested in Zend related content readers can view the site topics: "The introduction of the Zend Framework frame", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Course", "PHP object-oriented Programming Program , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design based on the Zend 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.