Home > Article > Backend Development > How to understand the php session operating mechanism
The php session operating mechanism is that the client passes the session id to the server, and the server then finds the corresponding file based on the session id and deserializes it to get the session value, and then serializes it when saving. Transform and then write
The knowledge point I will share today is the session running mechanism in PHP. It is not unfamiliar to developers who are familiar with PHP, but it is not very understandable for those who are just getting started. Next, in the article, I will introduce the session running mechanism in PHP in detail, which has a certain reference effect. I hope it will be helpful to everyone
## [Recommended course: PHP Tutorial]
PHP session operating mechanism meaning:
PHP session operating mechanism is that the client will use the session id Passed to the server, the server finds the corresponding file according to the session id. When reading, it deserializes the file content to get the session value. When saving, it is first serialized and then written toPHP session Mechanism classification
There are two mechanisms for session in PHP, namely the default mechanism and the user-defined session processing mechanism.Default mechanism
php.ini configuration:
session.save_handler = filesuses disk files to implement PHP sessions. It has the following It consists of several parts:
session_start()
session_start() is the beginning of the session mechanism, which has a certain probability of starting garbage collection. This probability is determined based on the configuration of php.ini, because in some systems session.gc_probability = 0, that is, the probability is 0, then there is no garbage collectionAssign value to $_session
Adding a new value will only be maintained in memory. When the script execution ends, write the value of $_session to the folder specified by session_id, and then close the related resources. At this stage, it is possible to perform operations to change the session_id, such as destroying an old session_id and generating a new session_id. This is generally used in custom session operationsExample:
if (isset($_COOKIE[session_name()])) { setcookie(session_name(),'',time() -42000,'/');//旧session cookie过期 } session_regenerate_id();//这一步会生成新的session_id //session_id()返回的是新的值
Write session operations
Destroy session
The cookies sent by the session are generally instant cookies, which are stored in memory and will expire when the browser is closed. , but if you just want to log out instead of closing the browser, then you need to destroy the session in the code. There are many methods. Example:1. setcookie(session_name(),session_id(),time() -8000000,..);//退出登录前执行 2. usset($_SESSION);//这会删除所有的$_SESSION数据,刷新后,有COOKIE传过来,但是没有数据。 3. session_destroy();//这个作用更彻底,删除$_SESSION 删除session文件,和session_id
##User-defined session processing mechanism
php.ini configuration
session.save_handler = user
The user-defined processing mechanism can be divided into the following parts
Execute open( $save_path,$session_name) statement opens the session operation handle
Execute read($id) to read data from it
Note:$save_path returns true directly in this case
End of script executionExecute write($id,$sess_data) statement
Destroy sessionIt should be noted that if the user needs to destroy the session, he must first execute destroy and then execute step 2
Summary: The above is the entire content of this article. I hope it will be helpful to everyone. .
php Chinese network learning topic
The above is the detailed content of How to understand the php session operating mechanism. For more information, please follow other related articles on the PHP Chinese website!