Current Location: Home> Latest Articles> The call order requirements for session_register_shutdown() and session_start()

The call order requirements for session_register_shutdown() and session_start()

M66 2025-05-25

Session management is a very important part of building web applications using PHP. Especially when it comes to the use of the two functions session_register_shutdown() and session_start() , many developers often confuse their call order, resulting in the Session data not being properly saved or other unpredictable behavior. This article will explore in-depth the role of these two functions, the call timing, and how to correctly organize the code to ensure the stable operation of the Session function.

Understand session_start() and session_register_shutdown()

session_start()

session_start() is a function used to start a session. It will read the Session file that already exists on the server and load the data into the $_SESSION hyperglobal array. If there is no corresponding Session file, it creates a new Session ID and initializes an empty Session.

Example:

 session_start();
$_SESSION['user'] = 'Alice';

session_register_shutdown()

session_register_shutdown() is a function provided by PHP to register a Session write-off hook, which ensures that Session data is automatically saved before the script ends. This is useful for scenarios where Session data needs to be written at the last minute of the script life cycle.

This function usually does not require manual call, because session_start() automatically registers it. However, it is especially important to understand and call it manually if you are using a custom Session processor, or have custom control over the Session lifecycle in some special frameworks.

Correct order of call

The most important rule is: session_start() must be called before all reads or writes to Session data, and should be called before session_register_shutdown() is registered (if you choose to register manually) .

Error example (will cause Session data to not be saved correctly):

 session_register_shutdown();
session_start(); // ? Incorrect order of call

Correct example:

 session_start();
session_register_shutdown(); // ? Correct order of call(If you need to register manually)

In most scenarios, just call session_start() , because PHP will automatically handle the registration shutdown hook. But in complex scripts, for example, you have a custom Session save mechanism:

 session_set_save_handler(
    [$handler, 'open'],
    [$handler, 'close'],
    [$handler, 'read'],
    [$handler, 'write'],
    [$handler, 'destroy'],
    [$handler, 'gc']
);
session_start();
session_register_shutdown(); // Explicit call,make sure session Write at the end of the script

Frequently Asked Questions

An error was reported when the output started

session_start() must be called before any output (such as echo or HTML), otherwise an error of "Cannot send session cookie - headers already sent" will appear. Make sure that there is no output of content or space before it.

Passing Session ID with URL (not recommended)

If the cookie is disabled, PHP appends the PHPSESSID parameter to the URL. Although not recommended, it is still used by someone in some cases, such as:

 $url = "https://m66.net/dashboard.php?" . SID;
echo "<a href=\"$url\">control Panel</a>";

Where SID is a constant provided by PHP, which is used to automatically append Session ID in the URL. Note that if the cookie is enabled, the SID is an empty string.

summary

  • Always call session_start() in front of the script.

  • If you use a custom Session save mechanism, you can explicitly call session_register_shutdown() , but the order should be after session_start() .

  • Avoid outputting content before session_start() .

  • Using URLs to pass Session IDs has security risks, so try to use cookies.

Correct Session management not only helps improve program stability, but also enhances security and user experience. I hope this article can help you better understand how these two functions are used.