The working principle of session and session usage _php tutorial

Source: Internet
Author: User
The working principle of session and session usage

Let's look at a session instance

function Getsessiondata ($session _name = ' sessid ', $session _save_handler = ' files ') {$session _data = array (); # did we get told what's 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 _nam         e);         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; }

And look at the session principle.
has been using the session to store data, has not been a good summary of the use of the session and how it works, today do a comb here.
The introduction here is mainly based on the PHP language, other language operations may be different, but the basic principle is unchanged.

1. How to operate the session in PHP:
Session_Start (); Use this function to open the session function
$_session//using predefined global variables to manipulate data
Use unset ($_session[' key ")//Destroy the value of a session
Simple to operate, everything is implemented by the server; because it's in the background, everything looks safe. But what mechanism does the session adopt, and how is it implemented, and how to maintain the state of the session?

2.session implementation and how it works
The browser and server use HTTP stateless communication, in order to maintain the state of the client, using the session to achieve this purpose. But how does the service end up labeling different clients or users?
Here we can use an example of life, if you attend a party, meet a lot of people, you will take what way to distinguish different people! You may be based on the face shape, or may be based on the user's name,
Or a person's identity card, that is, a unique logo. In the session mechanism, it also uses a unique session_id to mark different users, the difference is: the browser every request will be brought
The session_id generated by the server for it.
A brief introduction to the process: When the client accesses the server, the server sets the session according to the requirements, saves the conversation information on the server, and passes the session_id that marked the session to the client browser.
The browser keeps the session_id in memory (there are other ways of storing it, for example, written in a URL), which we call a cookie without expiration time. When the browser is closed, the cookie is cleared and there is no temporary cookie file for the user.
After the browser each request will add this parameter value, then the server according to this session_id, can obtain the client's data status.
If the client browser shuts down unexpectedly, the session data saved by the server is not released immediately, the data will still exist, as long as we know the session_id, we can continue to obtain the session information by request, but this time the backstage session still exists, But the session was saved with an expiration
Time, and when there is no client request beyond the specified time, he clears the session.
The following describes the session storage mechanism, the default session is saved in files, that is, the file to save the session data. In PHP mainly based on the configuration of php.ini Session.save_handler
To choose how you want to save the session.
Here by the way, if you want to do the server LVS, that is, more than one server, we generally use the memcached way session, otherwise it will cause some requests can not find the session.
A simple memcache configuration:
Session.save_handler = Memcache
Session.save_path = "tcp://10.28.41.84:10001"
Of course, if you must use the files file cache, we can make the file for NFS, and all the saved session files are located in one place.
Just now the Session-id returned to the user is finally saved in memory, where we can also set the parameters to save them in the user's URL.

3. Example Problems
Existing system A, B; Suppose a system is a web system that can run independently, that is, the session can be processed directly with the browser, and B system is mobile-based and needs to invoke the function interface of a system.
In the case where a does not change, that is, the login verification, the session storage is unchanged, the B system can handle the front-end user's request.
The solution provided here is to use PHP to implement
After the user login is successful, the Session-id of the saved session is returned to the B system, and then the B system requests the other interfaces with session_id.
A system is preceded by Session_Start with session_id (session_id);
So the B system can safely call a


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 was 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.html www.bkjia.com true http://www.bkjia.com/PHPjc/447021.html techarticle the session working principle and session usage first see 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.