Home  >  Article  >  Backend Development  >  Zend Framework implements multi-server sharing of SESSION data, zendsession_PHP tutorial

Zend Framework implements multi-server sharing of SESSION data, zendsession_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:56:15828browse

Zend Framework's method of realizing multi-server sharing of SESSION data, zendsession

This article describes the example of Zend Framework's method of realizing multi-server sharing of SESSION data. Share it with everyone for your reference, the details are as follows:

1. Origin of the problem

Large websites usually have multiple servers and use multiple second-level domain names. In this way, the session generated by one server cannot be shared by all servers. In this way, users cannot log in from one place

2. How PHP SESSION works

Before solving the problem, let’s first understand how PHP SESSION works. When the client (such as a browser) logs in to the website, the visited PHP page can use session_start() to open 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 the PHP program can learn the client's SESSION ID when requesting different pages; one is to automatically add the SESSION ID to the GET URL, or the POST form, by default. Under the first method, the variable name is PHPSESSID; the other method 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, SESSION data is saved by reading and writing files, and the directory where SESSION files are saved is specified by session.save_path, and the file name starts with sess_ is the prefix, followed by SESSION ID, 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. 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 a SESSION ID for the same client respectively. For example, for the same user browser, the SESSION ID generated by server A is 30de1e9de3192ba6ce2992d27a1b6a0a, The B server generates c72665af28a8b14c0fe11afe3b59b51b. In addition, PHP's SESSION data are stored separately in the file system of this server. As shown below:

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. COOKIE named PHPSESSID; the other is that the storage method/location of SESSION data must ensure that each server can access it. 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, the domain of each server will be different. The set COOKIE 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: aaa.infor96.com and www.infor96.com both belong to the domain .infor96.com, then we can Set the domain of the COOKIE to .infor96.com, so that aaa.infor96.com, www.infor96.com, etc. can access this COOKIE. The setting method in PHP code is as follows:

<&#63;php
ini_set('session.cookie_domain', '.infor96.com');
&#63;>

In this way, the purpose of each server sharing the same client SESSION ID is achieved.
The second goal can be achieved using file sharing methods, such as NFS, but the setup and operation are somewhat complicated. We can put data in memcache. In this way, each server can easily access the same data source and obtain the same SESSION data.
     
The solution is as shown below:

ok.

Readers who are interested in more zend-related content can check out the special topics of this site: "Zend FrameWork Framework Introductory Tutorial", "php Excellent Development Framework Summary", "Yii Framework Introduction and Summary of Common Techniques", "ThinkPHP Introductory Tutorial" , "php object-oriented programming introductory tutorial", "php mysql database operation introductory tutorial" and "php common database operation skills summary"

I hope this article will be helpful to everyone’s PHP programming based on the Zend Framework framework.

Articles you may be interested in:

  • Zend Framework tutorial Zend_Config_Xml usage analysis
  • Application analysis of Zend-based Config mechanism
  • Zend Framework tutorial Configuration file application.ini analysis
  • Zend Framework Smarty extension implementation method
  • Zend Framework routing mechanism code analysis
  • Zend Framework implements the method of storing session in memcache
  • Detailed explanation of usage of Zend Framework paging class
  • Zend Framework implementation of multi-file upload function example
  • Environment configuration for getting started with Zend Framework and the first Hello World example (with demo source code download)
  • Zend Framework Tutorial: How to connect to the database and perform add/delete queries (with demo source code download)
  • Zend Framework Tutorial: Zend_Config_Ini Usage Analysis

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1113717.htmlTechArticleZend Framework implements multi-server sharing of SESSION data, zendsession This article describes an example of Zend Framework implementing multi-server sharing of SESSION data method. Share it with everyone for everyone...
Statement:
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