Zend Framework implementation of multi-server sharing session data, zendsession_php tutorial

Source: Internet
Author: User
Tags php session zend framework

The Zend framework implements a method for sharing session data with multiple servers, zendsession


This paper describes the method of Zend framework to implement multi-server sharing session data. Share to everyone for your reference, as follows:

First, the origin of the problem

Large Web sites typically have multiple servers and use multiple level two domain names. The session generated by such a server cannot be shared by all servers. So that users can log in one place and not be able to pass all

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 session ID to the Get URL, or POST form, by default, the variable name is P Hpsessid; 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.S AVE_PATH specifies that the file name is prefixed with Sess_, followed by the SESSION ID, such as: sess_c72665af28a8b14c0fe11afe3b59b51b. 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 session IDs for the same client individually, such as the session ID generated by a server for the same user browser. 30DE1E9DE3192BA6CE2992D27A1B6A0A, while the B server generates C72665AF28A8B14C0FE11AFE3B59B51B. In addition, the SESSION data of PHP is stored separately in the file system of this server. As shown in the following:

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: Aaa.infor96.com and www.infor96.com belong to the domain. infor96.com, then we can set the COOKIE domain 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:

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

The purpose of each server sharing the same client SESSION ID is achieved.
The implementation of the second goal can use file sharing, such as NFS, but the setup, operation is somewhat complex. We can put the data in the memcache. This makes it easy for each server to access the same data source and get the same SESSION data.

The workaround is as follows:

Ok.

More interested in Zend related content readers can view the topic: "Zend framework of the introductory tutorial", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Tutorial", "PHP object-oriented Programming introduction tutorial "," Introduction to Php+mysql Database Operation "and" PHP common database Operation Skills Summary "

It is hoped that this article will help you to design PHP based on the Zend Framework framework.

Articles you may be interested in:

    • Zend_config_xml usage Analysis of Zend Framework Tutorial
    • Application analysis of config mechanism based on Zend
    • Zend Framework Tutorial configuration file Application.ini parsing
    • Zend Framework Smarty Extension Implementation method
    • Code analysis of routing mechanism of Zend framework framework
    • The Zend framework implements the method of storing the session in Memcache
    • Zend Framework Paging Class usage
    • Zend Framework implements multi-file upload function instances
    • Zend Framework Introduction Environment configuration and the first Hello World example (with demo source download)
    • Zend Framework Tutorial to connect the database and perform additions and deletions of the method (attached to the demo source download)
    • Zend_config_ini usage Analysis of Zend Framework Tutorial

http://www.bkjia.com/PHPjc/1113717.html www.bkjia.com true http://www.bkjia.com/PHPjc/1113717.html techarticle Zend Framework implements the method of multi-server sharing session data, Zendsession This example describes the Zend framework implementation of multi-server sharing session data. Share to everyone ...

  • Related Article

    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.