Current Location: Home> Latest Articles> Use session_register_shutdown() to realize automatic saving of user operations

Use session_register_shutdown() to realize automatic saving of user operations

M66 2025-05-24

1. Understand session_register_shutdown()

session_register_shutdown() is a function that registers the session writes the close hook. It automatically calls session_write_close() before the PHP script is executed. This means that at the last stage of script execution, the write behavior registered by the function will be executed regardless of whether session_start() is called or not.

This provides us with an opportunity to capture and save important data such as user edited content, form status, temporary settings, etc. as the user's session is about to end.

2. Examples of application scenarios

Suppose you are developing an online document editor. Users may suddenly close the browser or network interruption during editing. To avoid data loss, you want to automatically save the user's draft content before leaving.

You can implement the "temporary save" function by registering an automatic save logic in each request and using the session mechanism to retain data.

3. Specific implementation steps

Let's take a simple editor page as an example to demonstrate how to use session_register_shutdown() to implement the automatic save function.

1. Configure the PHP environment

Make sure your server has session support enabled and call session_start() at the beginning of the PHP file:

 <?php
session_start();

2. Register automatic save logic

Next, we use session_register_shutdown() to register a function to save the content currently edited by the user.

 function autoSaveDraft() {
    if (isset($_POST['content'])) {
        $_SESSION['autosave'] = $_POST['content'];
        file_put_contents('/tmp/autosave_' . session_id() . '.txt', $_POST['content']);
    }
}

session_register_shutdown('autoSaveDraft');

Here, we not only save the content to $_SESSION , but also write it to the temporary file /tmp/autosave_<session_id>.txt for subsequent recovery.

3. Build the form interface

Then, build a simple form in the page for the user to enter:

 <form method="POST" action="http://m66.net/editor.php">
    <textarea name="content" rows="10" cols="50"><?php echo htmlspecialchars($_SESSION['autosave'] ?? ''); ?></textarea>
    <br>
    <input type="submit" value="keep">
</form>

Every time the form is submitted, the content is written to the session and file through the automatic save mechanism, ensuring that even if the user leaves accidentally, the editing status can be restored when returning to the page.

4. Optional recovery logic

If you want to automatically restore the last draft of the user when the page is reopened, you can add the following logic when loading the page:

 if (file_exists('/tmp/autosave_' . session_id() . '.txt')) {
    $_SESSION['autosave'] = file_get_contents('/tmp/autosave_' . session_id() . '.txt');
}

This way, users can continue their editing work seamlessly without manually saving.