Home > Article > Backend Development > The working principle and usage of session_PHP tutorial
Session working principle and session usage
Let’s look at a session instance first
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; }
Look at the session principle again
I have been using sessions to store data, but I have never summarized the use of sessions and their working principles. Today I will summarize them here.
The introduction here is mainly based on the PHP language. The operations in other languages may be different, but the basic principles remain the same.
1. How to operate session in php:
session_start(); //Use this function to open the session function
$_session //Use predefined global variables to manipulate data
Use unset($_session['key']) //Destroy the value of a session
It's simple to operate, everything is done by the server; since the processing is in the background, everything also looks safe. But what mechanism does session use, how is it implemented, and how is the session state maintained?
2.session implementation and working principle
The browser and server use http stateless communication. In order to maintain the state of the client, session is used to achieve this purpose. But how does the server identify different clients or users?
Here we can use an example from life. If you attend a party and meet many people, how will you distinguish different people? You may base it on the face shape or the user’s name
Or a person's ID card, which uses a unique identification. In the session mechanism, such a unique session_id is also used to identify different users. The difference is: the browser will bring
with every request.
The session_id generated for it by the server.
Let’s briefly introduce the process: when the client accesses the server, the server sets the session according to the needs, saves the session information on the server, and passes the session_id indicating the session to the client browser,
The browser saves this session_id in memory (there are other storage methods, such as writing it in the URL), which we call a cookie without expiration time. After the browser is closed, this cookie will be cleared, and it will not contain the user's temporary cookie file.
In the future, the browser will add this parameter value to every request, and the server can obtain the client's data status based on this session_id.
If the client browser is closed unexpectedly, the session data saved by the server is not released immediately. The data will still exist at this time. As long as we know the session_id, we can continue to obtain the session information through requests; but at this time, the background session still exists. But the session save has an expiration
time, once there is no client request for more than the specified time, he will clear the session.
The following introduces the session storage mechanism. The default session is saved in files, that is, session data is saved in the form of files. In php, it is mainly based on the configuration of php.ini session.save_handler
to choose how to save the session.
By the way, if we want to use LVS of the server, that is, multiple servers, we generally use memcached session, 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 we must use files file caching, we can use nfs to store all session files in one place.
As mentioned just now, the session-id returned to the user is eventually saved in memory. Here we can also set parameters to save it in the user's URL.
3. Example problem
Existing systems a and b; Assume that system a is a web system that can run independently, that is, it can handle sessions directly with the browser. System b is based on mobile and needs to call the functional interface of system a.
While a remains unchanged, that is, login verification and session storage remain unchanged, system b can handle the front-end user's request.
The solution provided here is implemented using php
After the user successfully logs in, the session-id of the saved session is returned to system B, and then system B carries the session_id every time it requests other interfaces.
ASystem A adds session_id (session_id) before session_start;
In this way, system b 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 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