Home  >  Article  >  Backend Development  >  Detailed explanation of automatic recycling mechanism of Session in PHP

Detailed explanation of automatic recycling mechanism of Session in PHP

伊谢尔伦
伊谢尔伦Original
2017-04-26 17:44:552770browse

Under normal circumstances, you can destroy this session by clicking an "Exit" button provided on the page. However, if the user does not click the exit button, but directly closes the browser, or disconnects from the Internet, or directly shuts down the computer due to power outage, etc., the Session file saved on the server will not be deleted. Although you close the browser, you need to assign a new Session ID to log in again next time, but this is only because of the setting session.cookie_lifetime = 0 in php.ini to set the Session ID in the client Cookie The expiry date in specifies, in seconds, the lifetime of the cookie sent to the browser. A value of 0 means "until the browser is closed", and the default is 0.

When the system gives the Session a validity period, the Session ID will automatically disappear regardless of whether the browser is opened or not. The client's Session ID disappears, but the Session file saved on the server is not deleted. Therefore, server-side Session files that are not referenced by Session ID become "garbage". In order to prevent these junk Session files from causing excessive load on the system (because Session is not a semi-permanent existence like Cookie), the system has an automatic cleaning mechanism for Session files (junk files) that will never be used.

The Session file saved by the server is an ordinary text file, so it will have the modification time of the file. After the "Garbage Collection Program" is started, all expired Session files will be deleted based on the modification time of the Session file.

What kind of startup mechanism is the "garbage collection program"?

"Garbage collection program" is started when session_start()function is called. A website has multiple scripts, and each script needs to use the session_start() function to start a session, and there will be many users accessing it at the same time. This is very likely to cause the session_start() function to be called N times in 1 second. , and it would be unreasonable if the "garbage collection program" would be started every time. Even if the "garbage collection program" is started at least once every 15 minutes, it will be cleaned more than 100 times a day, which is too frequent. Set the probability of starting the garbage collection program by modifying the session.gc_probability and session.gc_divisor options in the php.ini file. The system will calculate the probability based on the session.gc_probability/session.gc_divisor formula, for example, option session.gc_probability = 1, option session.gc_divisor = 100, so the probability becomes 1/100, which is session_start() The "garbage collection program" will not be started until the function is called 100 times. Therefore, the more frequently the session page is accessed, the less likely it is to start. The general recommendation is to call it 1000-5000 times before it starts: 1/(1000~5000).

The above is the detailed content of Detailed explanation of automatic recycling mechanism of Session in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn