After starting a session with session_start() in PHP, we usually do not need to explicitly call session_write_close() to save session data. Even if the script exits midway or ends execution, PHP will automatically save the data in $_SESSION . Behind this "auto-save" mechanism, it actually relies on a very critical registration mechanism - session_register_shutdown() .
This article will deeply analyze how the session_register_shutdown() function works and its role in the PHP life cycle, helping you to understand PHP's session automatic saving process more clearly.
session_register_shutdown() is a function provided by PHP that registers a shutdown function after the session is started. This function will be automatically called when the script is executed (or aborted) and is used to save the current session data. Its core function is to ensure that the data in $_SESSION can still be persisted even if the script exits abnormally.
Equivalently, we can also understand it as:
session_register_shutdown();
// Equivalent to:
register_shutdown_function('session_write_close');
In other words, the internal session_register_shutdown() actually encapsulates the behavior of register_shutdown_function('session_write_close') .
By default, when you call session_start() to start a session, PHP will automatically register session_register_shutdown() . Let's look at a simple example:
session_start();
$_SESSION['user'] = 'admin';
exit;
Even if exit is called midway and the script does not call session_write_close() manually, the user=admin information will be successfully written to the session file. This is precisely because the function registered by session_register_shutdown() is executed in the shutdown stage.
PHP provides register_shutdown_function() to register a callback when the script is executed. These callbacks are executed in the following situations:
The script has been executed normally
Call exit() or die() in the script
Fatal errors occurred in the script (such as memory exhaustion, etc., depending on the situation)
The shutdown process of PHP is roughly as follows:
All script logic has been executed
Execute the registered shutdown functions (including session_write_close )
Cleanup work such as destroying variables, releasing resources, etc.
Output content to the client
Therefore, the function registered by session_register_shutdown() will be called in this step to ensure that the session data can be saved correctly in any exit mode.
In PHP's source code implementation, session_register_shutdown() actually registers session_write_close() as a shutdown callback. Its core logic can be found in ext/session/session.c:
PHP_FUNCTION(session_register_shutdown)
{
php_session_register_shutdown();
}
And php_session_register_shutdown() will call:
register_shutdown_function("session_write_close", NULL);
Therefore, the essence is to register an operation function that automatically saves the session and we manually write:
register_shutdown_function('session_write_close');
It is completely equivalent.
Although PHP will automatically call session_write_close() , manually calling it in some scenarios is an optimization method. For example:
Release the lock in advance : PHP's session is based on file lock. If you do not call session_write_close() , the session file will be locked until the script ends, possibly blocking other requests.
Concurrent request : If you have multiple concurrent requests that need to operate session but do not depend on each other, you can immediately session_write_close() after reading $_SESSION to release the lock to improve concurrency.
Improve performance : Releasing sessions as early as possible in long-running scripts can avoid unnecessary resource consumption.
Example:
session_start();
$user = $_SESSION['user'];
session_write_close(); // Release the lock in advance
// The subsequent logic will not affect session data
file_get_contents('https://m66.net/api/do-something');
Behind PHP's session automatic saving mechanism is implemented through the session_register_shutdown() function. It registers a shutdown function to automatically call session_write_close() before the script ends, thus ensuring that session data is saved correctly.
Understanding this mechanism not only helps us write more robust code, but also provides effective solutions in performance optimization (such as in high concurrency environments). Proficient in mastering these underlying details is an important cultivation for every senior PHP developer.