Case Analysis of PHP session Garbage Collection Mechanism

  • 2021-12-12 04:02:27
  • OfStack

This article illustrates the PHP session garbage collection mechanism. Share it for your reference, as follows:

session expiration time

There is one such configuration in the php. ini file, This configuration indicates the expiration time of session file. By default, it is 1440 seconds, that is, 24 minutes. This 24 minutes is the daze time of session. If session is not operated within 24 minutes, the session file will expire. If session is operated within 23 minutes, it will expire for another 24 minutes. If it expires, the session will be considered as garbage by the server.


session.gc_maxlifetime = 1440

Garbage collection mechanism control

There are also two configurations where the server each time session_start It is possible to start the garbage collection mechanism to delete garbage files, and the probability is session.gc_probability / session.gc_divisor .


session.gc_probability = 1
session.gc_divisor = 1000

Case

We can configure this way to experience the garbage collection mechanism of session under 1.


session.gc_maxlifetime = 60
session.gc_probability = 1000
session.gc_divisor = 1000

In this way, the expiration time of session is 60 seconds, and gc will be turned on every time session_start, and then the garbage will be deleted.

Then write a script: gc. php


<?php
session_start();

Test

1. Then start the browser for the first time to request gc. php, and then close the browser. At this time, the first session file will be generated. If it is not configured, it is in C:\ Window\ Temp directory by default, which can also be configured.

2. Start the browser for the second time to request gc. php, and then close the browser. At this time, the second session file will be generated

3. Start the browser for the third time to request gc. php, do not close the browser, and then constantly refresh gc. php, will call session_start Method, because we configured it accordingly, every time session_start Will call gc, and then gc to check whether there are junk files under 1. If there are junk files, delete them. We will see that the first two files are deleted one by one.

For more readers interested in PHP related content, please check the topics on this site: "Summary of php Cache Technology", "Encyclopedia of PHP Array (Array) Operation Skills", "Summary of php String (string) Usage", "Summary of PHP Error and Exception Handling Methods", "Introduction to php Object-Oriented Programming", "Introduction to php+mysql Database Operation" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: