Use Zend framework to build a website (7)-implement multi-server sharing session data

Source: Internet
Author: User
Tags zend framework
Use Zend framework to build a website (7) -- implement multi-server sharing session data 2008/12/07 p. M.
Refer:Xiao LidaHttp://nio.infor96.com/sharing-php-session-data-between-servers

I. Problem Origin


A large website usually has multiple servers and uses multiple second-level domain names. In this way, the session generated by a server cannot be shared by all servers. In this way, users cannot log on to the same region.
(The following are fromHttp://nio.infor96.com/sharing-php-session-data-between-servers, I will slightly modify)

IIWorking principle of PHP session

Before solving the problem, let's take a look at the working principles of the PHP session. When a client (such as a browser) logs on to a website, you can use session_start () to open the session on the accessed PHP page, this will generate a unique session ID of the client (this ID can be obtained/set through the session_id () function ). The session ID can be retained on the client in two ways, so that the PHP program can obtain the session ID of the client when requesting different pages; one is to automatically add the session ID to the get URL, or
In the form of post, the variable name is PHPSESSID by default, and the session ID is saved in the cookie through cookie. By default, the cookie name is PHPSESSID. Here we mainly describe the cookie method, because it is widely used.


Where can session data be stored? Of course, it is on the server side, but not stored in the memory, but saved in a file or database. By default, PHP. the session storage method set in INI is files (Session. save_handler = files), that is, session data is saved by reading and writing files, and the session file directory is saved by session. save_path is specified. The file name is prefixed with sess _ and followed by the session ID. For example, sess_c000065af28a8b14c0fe11afe3b59b51b. The data in the file is serialized.
Session data. If the traffic volume is large, many session files may be generated. You can set a hierarchical directory to store session files, which improves the efficiency. The setting method is session. save_path = "N;/save_path", where N is the classification level, and save_path is the start directory. When writing session data, PHP will get the session_id of the client, and then find the corresponding session file in the saved directory of the specified session file based on the session ID. If the session file does not exist, it will be created, finally, the data is serialized and written to the file. Read
Session data is a similar operation process. The read data needs to be deserialized to generate the corresponding session variable.

Iii. Main obstacles and solutions to multi-server session sharing

By understanding the working principle of the session, we can find that by default, each server generates a session ID for the same client, for example, for the same user browser, the session ID generated by server a is 30de1e9de3192ba6ce2992d27a1b6a0a, while that generated by server B is c000065af28a8b14c0fe11afe3b59b51b. In addition, the session data of PHP is stored in the file system of the current server. As shown in:


After confirming the problem, you can start to solve it. To share session data, you must achieve two goals: one is that the session IDs generated by each server on the same client must be the same and can be transmitted through the same cookie, that is to say, each server must be able to read the same cookie named PHPSESSID; the other is the session data storage method/location, which must be accessible to each server. To put it simply, multiple servers share the client's
Session ID. You must also share the session data on the server.

The implementation of the first target is actually very simple. You only need to set the cookie domain. By default, the cookie domain is the domain name/IP address of the current server, if the domain is different, the cookie set by each server cannot access each other. For example, the server www.aaa.com cannot read or write the cookie set by the server www.bbb.com.
Cookie. The servers of the same website have their own special characteristics, that is, they belong to the same level-1 domain. For example, both aaa.infor96.com and www.infor96.com belong to the domain .infor96.com, then we can set the cookie domain to .infor96.com, so that aaa.infor96.com and www.infor96.com
And so on. The setting method in PHP code is as follows:


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

In this way, each server shares the same client session ID.

The implementation of the second target can use the file sharing method, such as the NFS method, but the settings and operations are somewhat complicated. We can store data in memcache. In this way, each server can easily access the same data source and obtain the same session data.

For more information, see my other article: http://hi.baidu.com/billdkj/blog/item/049d04f0b2a896c47831aa1c.html.



The solution is shown in:

OK.

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.