Session Management of Zendframework

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

The working principle of 1,php session.
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.
The session's data is saved 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.

The management of session in 2,zendframework.
The Zend_session component wraps a PHP-built session module, provides an interface for managing sessions, and provides APIs for Zend_session_namespace persisted namespace data.
The Zend_session_namespace instance implements the $_session namespace in the form of object storage.
The term "session data" represents the data that is stored in $_session on the server side. The data is managed using Zend_session, which is controlled by the Zend_session_namespace in an object manner.

1, add the following configuration in the Application.ini:

; session-related configuration

Resources.session.save_path = Application_path "/... /data/session "
resources.session.use_only_cookies = true
resources.session.name =" Hwsportal "
Resources.session.remember_me_seconds = 864000


The configuration options for other items can be referred to: http://framework.zend.com/manual/zh/zend.session.global_session_management.html

2, in the Bootstrap.php Unified enable session

protected function _initsession ()
        {
                $options = $this->getoption (' resources ');
                $options = $options [' Session '];
                Zend_session::setoptions ($options);
                Zend_session::start ();
        }


3. Save session data in code

$namespace = new Zend_session_namespace (' MyNamespace ');
$namespace->user = "MyUserName";

4. Session Access:

$_session[' MyNamespace ' [' User '];//value is: myusername;

Note: The method provided by Zend_session is already basically enough, can encapsulate a class by itself, inherit from Zend_session, add own method in the encapsulated class, the above processing is the case that the session data is saved in the file, If you want the session data to explode in the database, you need to use the Zend_session_savehandler_dbtable class, refer to the http://framework.zend.com/manual/zh/ Zend.session.savehandler.dbtable.html

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.