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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Analysis of the role and principle of nohup In Unix and Unix-like operating systems, nohup is a commonly used command that is used to run commands in the background. Even if the user exits the current session or closes the terminal window, the command can still continue to be executed. In this article, we will analyze the function and principle of the nohup command in detail. 1. The role of nohup: Running commands in the background: Through the nohup command, we can let long-running commands continue to execute in the background without being affected by the user exiting the terminal session. This needs to be run

WPS is a commonly used office software suite, and the WPS table function is widely used for data processing and calculations. In the WPS table, there is a very useful function, the DATEDIF function, which is used to calculate the time difference between two dates. The DATEDIF function is the abbreviation of the English word DateDifference. Its syntax is as follows: DATEDIF(start_date,end_date,unit) where start_date represents the starting date.

MyBatis is a popular Java persistence layer framework that is widely used in various Java projects. Among them, batch insertion is a common operation that can effectively improve the performance of database operations. This article will deeply explore the implementation principle of batch Insert in MyBatis, and analyze it in detail with specific code examples. Batch Insert in MyBatis In MyBatis, batch Insert operations are usually implemented using dynamic SQL. By constructing a line S containing multiple inserted values

The ISNULL() function in MySQL is a function used to determine whether a specified expression or column is NULL. It returns a Boolean value, 1 if the expression is NULL, 0 otherwise. The ISNULL() function can be used in the SELECT statement or for conditional judgment in the WHERE clause. 1. The basic syntax of the ISNULL() function: ISNULL(expression) where expression is the expression to determine whether it is NULL or

MyBatis is an excellent persistence layer framework. It supports database operations based on XML and annotations. It is simple and easy to use. It also provides a rich plug-in mechanism. Among them, the paging plug-in is one of the more frequently used plug-ins. This article will delve into the principles of the MyBatis paging plug-in and illustrate it with specific code examples. 1. Paging plug-in principle MyBatis itself does not provide native paging function, but you can use plug-ins to implement paging queries. The principle of paging plug-in is mainly to intercept MyBatis

Usage of Transform in CSS The Transform property of CSS is a very powerful tool that can perform operations such as translation, rotation, scaling and tilting of HTML elements. It can dramatically change the appearance of elements and make web pages more creative and dynamic. In this article, we will introduce the various uses of Transform in detail and provide specific code examples. 1. Translate (Translate) Translate refers to moving an element a specified distance along the x-axis and y-axis. Its syntax is as follows: tran

The RPM (RedHatPackageManager) tool in Linux systems is a powerful tool for installing, upgrading, uninstalling and managing system software packages. It is a commonly used software package management tool in RedHatLinux systems and is also used by many other Linux distributions. The role of the RPM tool is very important. It allows system administrators and users to easily manage software packages on the system. Through RPM, users can easily install new software packages and upgrade existing software

The chage command in the Linux system is a command used to modify the password expiration date of a user account. It can also be used to modify the longest and shortest usable date of the account. This command plays a very important role in managing user account security. It can effectively control the usage period of user passwords and enhance system security. How to use the chage command: The basic syntax of the chage command is: chage [option] user name. For example, to modify the password expiration date of user "testuser", you can use the following command
