Current Location: Home> Latest Articles> What's the Difference Between SessionHandler::close and session_write_close? Which One Is Right for You?

What's the Difference Between SessionHandler::close and session_write_close? Which One Is Right for You?

gitbox 2025-09-16

When handling sessions in PHP, you often encounter two seemingly related functions: SessionHandler::close() and session_write_close(). Despite their similar names and association with closing sessions, their purposes, usage timing, and underlying mechanisms differ. This article explores the differences between these two functions, helping you select the most appropriate approach for your needs.

1. Basic Concepts

session_write_close()

session_write_close() is a global function used to manually end the write operations of the current session. By default, PHP automatically closes the session at the end of the script, but in certain scenarios (such as using multi-threading or needing to release locks early), you may need to call it explicitly.

This function performs the following actions:

  • Writes and saves session data.

  • Releases the lock on the session file, allowing other requests to access the user's session.

Once called, the current script can no longer write new data to $_SESSION, though reading remains possible.

SessionHandler::close()

SessionHandler::close() is a method in PHP's SessionHandler class. It is not typically called directly by developers, but is invoked internally by PHP at the end of the session lifecycle.

When using a custom session handler (such as by extending SessionHandlerInterface or SessionHandler), you need to implement this method to define actions to perform when the session closes, such as closing database connections or writing logs.

It is part of the session_set_save_handler() registration, which defines the handling process of the session lifecycle.

2. Comparative Analysis

Featuresession_write_close()SessionHandler::close()
TypeGlobal functionClass method
Called by developerYesNo (called internally by PHP)
Primary purposeSave session data and release lockDefine custom session closing behavior
Applicable to custom session handlersNoYes
Use caseRelease session lock earlyImplement session lifecycle management

Example Usage

Scenario using session_write_close():

<?php
session_start();
// Set some session data
$_SESSION['user_id'] = 123;
<p>// Release the lock early<br>
session_write_close();</p>
<p>// Continue with other operations that don’t affect the session, such as concurrent requests or output<br>
echo "Session closed early.";<br>
?>

Scenario using custom SessionHandler::close():

<?php
class MySessionHandler extends SessionHandler {
    public function close(): bool {
        // Custom logic, e.g., close database connection
        // or write logs
        return parent::close();
    }
}
<p>$handler = new MySessionHandler();<br>
session_set_save_handler($handler, true);<br>
session_start();<br>
$_SESSION['foo'] = 'bar';<br>
?>

In this example, close() is automatically called by PHP at the end of the session lifecycle.

3. Which One Is Right for You?

It depends on your application scenario:

  • If you are using the default session mechanism and just need to release the session lock early to allow other requests to continue, you should use session_write_close().

  • If you are implementing a custom session storage mechanism (such as Redis, database, or encrypted storage) and need to control the behavior at each stage of the session lifecycle, you need to implement SessionHandler::close().

In short:

session_write_close() is for controlling behavior; SessionHandler::close() is for defining behavior.

4. Conclusion

Although both functions relate to “closing” sessions, their responsibilities and usage differ significantly. Understanding the differences helps you write more stable and performant PHP applications. In daily development, properly using session_write_close() can effectively prevent performance bottlenecks caused by session locks, while mastering SessionHandler::close() forms the foundation for advanced session management mechanisms.