Current Location: Home> Latest Articles> How to correctly use session_register_shutdown() in PHP

How to correctly use session_register_shutdown() in PHP

M66 2025-05-28

In PHP development, session_register_shutdown() is a relatively useless but very practical function, especially when it involves automatic saving and closing of session data. This article will explore in-depth the use of this function, usage scenarios, and best practices.

What is session_register_shutdown() ?

session_register_shutdown() is a built-in PHP function that automatically calls session_write_close() when script execution ends. Its main function is to ensure that the session data is correctly written and closed after the script is executed, avoiding data loss or locking problems.

In other words, calling this function is equivalent to registering a session closing hook executed when a script terminates. Using this function is safer than calling session_write_close() manually, and is especially suitable for ensuring that session data is safely written to the server after complex business logic or long connection processing.

Example of usage

 <?php
session_start();
session_register_shutdown();

// Suppose we record user behavior logs
$_SESSION['last_action'] = time();

// There may be many logical processing in the future,Maybe even ahead of time exit
if ($_GET['debug'] === '1') {
    exit("Debug mode,Do not continue to execute");
}

// The script continues to perform other operations
?>

In the above code, even if exit() is used midway, it can ensure that the session data is correctly written when the script terminates.

Why not always call session_write_close() manually?

While you can call session_write_close() manually at some point in your code, this requires you to know exactly when the session data will not be changed again. In large projects or scripts with multiple conditional branches, this manual processing is prone to misses or errors.

Using session_register_shutdown() can ensure that session_write_close() is called uniformly when the script terminates, especially for the following scenarios:

  • When the script is terminated early in exception or error;

  • in output cache or asynchronous task processing;

  • In a branch or middleware structure involving complex condition.

Used with output cache

When combining output buffering functions such as ob_start() and ob_end_flush() ), session_register_shutdown() can ensure the correct order of output and session writing, and avoid session saving failure after sending header information.

 <?php
session_start();
session_register_shutdown();
ob_start();

$_SESSION['visit_count'] = ($_SESSION['visit_count'] ?? 0) + 1;

echo "Welcome to visit,This is your first {$_SESSION['visit_count']} Visits。";

ob_end_flush();
?>

Things to note

  1. PHP version : session_register_shutdown() was introduced since PHP 5.4. If your server is still using older versions, please do not use them.

  2. Automatic call limit : This function can only be used after the session starts (i.e. after session_start() ), otherwise it is invalid.

  3. Avoid repeated calls : Generally, you only need to call once. Repeated calls will not cause errors, but they also have no practical significance.

Practical advice

In the framework or custom portal file you develop (for example, index.php ), it is recommended to use session_register_shutdown() to register the session end hook. This can reduce the need to explicitly call session_write_close() in business logic, and improve the robustness and maintenance of the code.

In addition, if your website involves user login status judgments or behavior records, be sure to ensure that session data can be written stably. For example: