Current Location: Home> Latest Articles> How to Use the session_register_shutdown Function to Safely Save Session Data After the User Closes the Page?

How to Use the session_register_shutdown Function to Safely Save Session Data After the User Closes the Page?

M66 2025-06-23

When building web applications with PHP, managing and saving session data is a crucial part of the process. Typically, we want session data to be securely saved even if the user closes the browser or page, ensuring data consistency and security. The session_register_shutdown() function is a tool designed for this purpose.

What is session_register_shutdown?

session_register_shutdown() is a built-in PHP function that automatically calls session_write_close() when the script finishes executing. This ensures that session data is safely written to the server, whether the script finishes normally or is unexpectedly terminated.

By default, PHP automatically saves session data when the script ends. However, in certain situations, such as when register_shutdown_function() is enabled and session operations are called within it, or when functions like exit() or die() are called during script execution, session data may not be saved correctly. In such cases, using session_register_shutdown() becomes especially important.

How to Use It?

The usage is very simple. After calling session_start(), you just need to call session_register_shutdown() once:

<?php
session_start();
session_register_shutdown();
<p>// Example: Increase user visit count<br>
if (!isset($_SESSION['views'])) {<br>
$_SESSION['views'] = 1;<br>
} else {<br>
$_SESSION['views']++;<br>
}</p>
<p>echo "You have visited this page {$_SESSION['views']} times.";<br>
?><br>

The above code ensures that session data is saved regardless of how the page is closed (normal closure, unexpected termination, etc.).

Working with Custom Shutdown Functions

In some advanced scenarios, you may register custom shutdown functions, such as logging user activity or cleaning up resources. In these cases, calling session_register_shutdown() can be used in conjunction with register_shutdown_function() to prevent session data loss.

<?php
session_start();
session_register_shutdown();
<p>register_shutdown_function(function () {<br>
// Log to remote server<br>
file_get_contents("<a rel="noopener" target="_new" class="" href="https://m66.net/log?uid=">https://m66.net/log?uid=</a>" . urlencode($_SESSION['user_id']));<br>
});</p>
<p>// Normal session logic<br>
$_SESSION['last_action'] = time();<br>
?><br>

As shown above, the user's action logs are sent to https://m66.net/log, which helps you capture the last behavior of the user even before they close the page.

Things to Keep in Mind

  1. session_register_shutdown() is effective only when the session is already started, so make sure to call it after session_start().

  2. It is not recommended to modify session data inside the shutdown function, as the shutdown phase is mainly for reading or writing external resources.

  3. This function is available in PHP 7+ versions; earlier versions may not support it.

Conclusion

session_register_shutdown() is a simple yet extremely useful function, especially for applications that require high data consistency. By using it correctly, you can effectively prevent session data loss caused by unexpected page closures, thereby improving user experience and system robustness.