Session working principle and session usage _ PHP Tutorial

Source: Internet
Author: User
The working principle and usage of the session. Session working principle and session usage first let's look at a session instance functiongetsessiondata ($ session_namesessid, $ session_save_handlerfiles) {$ session_dataarray (); # di session working principle and session usage

First, let's look at a session instance.

function getsessiondata ($session_name = ' sessid', $session_save_handler = 'files') {     $session_data = array();     # did we get told what the old session id was? we can't continue it without that info     if (array_key_exists($session_name, $_cookie)) {         # save current session id         $session_id = $_cookie[$session_name];         $old_session_id = session_id();                 # write and close current session         session_write_close();                 # grab old save handler, and switch to files         $old_session_save_handler = ini_get('session.save_handler');         ini_set('session.save_handler', $session_save_handler);                 # now we can switch the session over, capturing the old session name         $old_session_name = session_name($session_name);         session_id($session_id);         session_start();                 # get the desired session data         $session_data = $_session;                 # close this session, switch back to the original handler, then restart the old session         session_write_close();         ini_set('session.save_handler', $old_session_save_handler);         session_name($old_session_name);         session_id($old_session_id);         session_start();     }         # now return the data we just retrieved     return $session_data; }

Let's look at the session principle.
I have been using session to store data. I have never summarized the usage and working principles of the session. I will sort it out here today.
The introduction here is mainly based on the php language. operations in other languages may be different, but the basic principle remains unchanged.

1. how to operate the session in php:
Session_start (); // use this function to enable the session function.
$ _ Session // use predefined global variables to operate data
Use unset ($ _ session ['key']) // destroy the value of a session
In simple operations, everything is implemented by the server; because the processing is in the background, everything looks safe. But what kind of mechanism does the session adopt and how does it maintain the session state?

2. session implementation and working principle
The browser and server use http stateless communication. to maintain the client status, use session to achieve this goal. But how does the server mark different clients or users?
Here we can use an example in our daily life. if you have met many people at a party, how will you differentiate different people! Depending on your face or user name,
Or a person's ID card, that is, a unique identifier. In the session mechanism, a unique session_id is used to mark different users. The difference is that each request by the browser will contain
Session_id generated by the server.
A brief introduction of the process: when the client accesses the server, the server sets the session as needed, stores the session information on the server, and transmits the session_id of the session to the client browser,
The browser saves the session_id in the memory (there are other storage methods, such as writing it in a url), which is called a cookie with no expiration time. After the browser is closed, the cookie is cleared, and no temporary cookie file exists.
In the future, the browser will add this parameter value for each request, and the server will be able to obtain the client data status based on this session_id.
If the browser of the client closes unexpectedly, the session data stored on the server is not immediately released, and the data will still exist. as long as we know the session_id, we can continue to obtain the session information through the request; however, the background session still exists at this time, but the session is saved with an expiration date.
Time. when no client request is received after the specified time, the session is cleared.
The following describes the session storage mechanism. The Default session is saved in files, that is, session data is saved as a file. In php, session. save_handler is configured based on php. ini.
To save the session.
Here, by the way, if we want to use server lvs, that is, multiple servers, we generally use the memcached session method. otherwise, some requests will not be able to find the session.
A simple memcache configuration:
Session. save_handler = memcache
Session. save_path = "tcp: // 10.28.41.84: 10001"
Of course, if files must be cached, we can make the file nfs and locate all the stored session files in one place.
The session-id returned to the user is stored in the memory. here we can also set the parameter to save it in the user url.

3. instance problems
Existing systems a and B. assume that system a is a web system that can run independently and can directly process sessions with the browser. System B is mobile-based and needs to call the functional interfaces of System,
If a does not change, that is, login verification and session storage remain unchanged, B can process the requests of the front-end users.
The solution provided here is implemented using php
After the user logs in successfully, the session-id of the saved session is returned to system B. Then, every time System B requests other interfaces, it carries session_id.
System A adds session_id (session_id) before session_start );
In this way, System B can safely call.


The session function also has

Session_cache_expire-return current cache expire
Session_cache_limiter-get and/or set the current cache limiter
Session_commit-alias of session_write_close
Session_decode-decodes session data from a string
Session_destroy-destroys all data registered to a session
Session_encode-encodes the current session data as a string
Session_get_cookie_params-get the session cookie parameters
Session_id-get and/or set the current session id
Session_is_registered-find out whether a global variable is registered in a session
Session_module_name-get and/or set the current session module
Session_name-get and/or set the current session name
Session_regenerate_id-update the current session id with a newly generated one
Session_register-register one or more global variables with the current session
Session_save_path-get and/or set the current session save path
Session_set_cookie_params-set the session cookie parameters
Session_set_save_handler-sets user-level session storage functions
Session_start-initialize session data
Session_unregister-unregister a global variable from the current session
Session_unset-free all session variables
Session_write_close-write session data and end session


Http://www.bkjia.com/PHPjc/447021.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/447021.htmlTechArticlesession working principle and session usage first let's look at a session instance function getsessiondata ($ session_name = sessid, $ session_save_handler = files) {$ session_data = array (); # di...

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.