search
HomeBackend DevelopmentPHP Tutorialphp session recycling mechanism

php session recycling mechanism

Nov 22, 2016 pm 04:22 PM
php

Due to the working mechanism of PHP, it does not have a daemon thread to scan session information regularly and determine whether it is invalid. When a valid request occurs, PHP will decide whether to start a GC (Garbage Collector) based on the value of the global variable session.gc_probability/session.gc_divisor (which can also be modified through the php.ini or ini_set() function). By default, session.gc_probability = 1, session.gc_divisor = 100, which means there is a 1% probability that GC will be started.

The job of GC is to scan all session information, subtract the last modification time (modified date) of the session from the current time, and compare it with the session.gc_maxlifetime parameter. If the survival time has exceeded gc_maxlifetime, delete the session.

Then why does gc_maxlifetime become invalid?

By default, session information will be saved in the system's temporary file directory in the form of text files. Under Linux, this path is usually tmp, and under Windows it is usually C:WindowsTemp. When there are multiple PHP applications on the server, they will save their session files in the same directory. Similarly, these PHP applications will also start GC at a certain probability and scan all session files.

The problem is that when GC is working, it does not distinguish between sessions on different sites. For example, site A's gc_maxlifetime is set to 2 hours, and site B's gc_maxlifetime is set to the default 24 minutes. When the GC of site B starts, it will scan the public temporary file directory and delete all session files older than 24 minutes, regardless of whether they come from site A or B. In this way, the gc_maxlifetime setting of site A is useless.

Once you find the problem, it’s easy to solve it. Modify the session.save_path parameter, or use the session_save_path() function to point the directory where the session is saved to a dedicated directory. The gc_maxlifetime parameter works normally.

Another problem is that gc_maxlifetime can only guarantee the shortest time for the session to survive, and cannot be saved. After this time, the session information will be deleted immediately. Because GC is started based on probability and may not be started for a long period of time, a large number of sessions will still be valid after exceeding gc_maxlifetime. One way to solve this problem is to increase the probability of session.gc_probability/session.gc_divisor. If it is raised to 100%, this problem will be completely solved, but it will obviously have a serious impact on performance. Another method is to determine the lifetime of the current session in your code. If it exceeds gc_maxlifetime, clear the current session.

php session GC function is Garbage Collector. When this GC starts, it will clear those sessions that have "timed out". It works like this:

The user visits and logs in to the website. At this time, the background will call session_start to try to generate a session (if there is already a session, it is equivalent to a valid session request)

For each valid session like this Request, apache's php module will calculate the probability of starting GC based on the session-related global variable gc_probability/gc_divisor =>, and use this probability to decide whether GC should be started in this request. For example, the default value of session.gc_probability is 1, and the default value of session.gc_divisor is 100, then the probability of starting the "garbage collection" is 1%, which means that for every 100 requests, there will It is possible to clean up an expired session

If GC is started, GC will scan all session files under the path of the current session (session.save_path), and determine which sessions have expired based on another global variable session.gc_maxlifetime ("current The difference between "time" and "atime or mtime of the session file" is greater than gc_maxlifetime: expired), and delete these expired sessions

If you do not have any interactive operations for a long time after starting a session (for example, non-stop coding words, not submitted or saved as draft), then your session file saved in the background will not have the opportunity to be modified or accessed. After gc_maxlifetime (default value 1440 seconds = 24 minutes), it may be invalid. After being cleared, if you submit again after this, an error will be reported because the session has expired

It can be seen that gc_maxlifetime is set to 24 minutes, which is not enough for writing some articles. This is one reason. In addition, the default path of session.save_path is /tmp on Linux, and few programs will modify this setting. If there are multiple virtual hosts on this server, many session files with different session_names will be stored in the /tmp directory. What's bad is that PHP's GC does not distinguish session ownership. It will clean up all expired session files in this directory based on the gc_maxlifetime it obtains.

According to the above analysis, the solution is: UTBLOG adds a statement in the .htaccess file, expands the local value of session.gc_maxlifetime to 14400 (4 hours), and sets session.save_path to /tmp/utblog in the background. In this way, utblog's session file will not be interfered by other websites, and the expiration time of 4 hours, I think, should be enough anyway.

After testing it, everything is as I expected.

Also, of course it is also possible to directly change /etc/php.ini. If you do not have permission to change php.ini or apache's conf file, and .htaccess is prohibited, then directly modify the sessionmanager.class.php file of plog and add ini_alter("session.gc_maxlifetime", 14400) before the session_start line. Can. The plog structure is good, only this part calls session_start, so only this part needs to be modified. I've tested it locally and it works.

------------------------------------------------ --------------------------

session.gc_probability integer

session.gc_probability and session.gc_divisor are used together to manage gc (garbage collection Garbage collection) process startup probability. Default is 1. See session.gc_divisor for details.

session.gc_divisor integer

session.gc_divisor and session.gc_probability together define the probability of starting the gc (garbage collection garbage collection) process when each session is initialized. This probability is calculated using gc_probability/gc_divisor. For example 1/100 means there is a 1% probability of starting the gc process on each request. session.gc_divisor defaults to 100.

session.gc_maxlifetime integer

session.gc_maxlifetime specifies the number of seconds after which the data will be considered "garbage" and cleared.

Note:

If different scripts have different session.gc_maxlifetime values ​​but share the same place to store session data, the script with the smallest value will clean the data. In this case, use this directive together with session.save_path.

Note: If using the default file-based session handler, the file system must keep track of access time (atime). The Windows FAT file system does not work, so if you must use a FAT file system or another file system that cannot track atime, you will have to find another way to handle garbage collection of session data. Since PHP 4.2.3 mtime (modification time) has been used instead of atime. Therefore, it is no problem for file systems that cannot track atime.


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
PHP's Purpose: Building Dynamic WebsitesPHP's Purpose: Building Dynamic WebsitesApr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP: Handling Databases and Server-Side LogicPHP: Handling Databases and Server-Side LogicApr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

How do you prevent SQL Injection in PHP? (Prepared statements, PDO)How do you prevent SQL Injection in PHP? (Prepared statements, PDO)Apr 15, 2025 am 12:15 AM

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python: Code Examples and ComparisonPHP and Python: Code Examples and ComparisonApr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP in Action: Real-World Examples and ApplicationsPHP in Action: Real-World Examples and ApplicationsApr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: Creating Interactive Web Content with EasePHP: Creating Interactive Web Content with EaseApr 14, 2025 am 12:15 AM

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python: Comparing Two Popular Programming LanguagesPHP and Python: Comparing Two Popular Programming LanguagesApr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The Enduring Relevance of PHP: Is It Still Alive?The Enduring Relevance of PHP: Is It Still Alive?Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.