Why Calling session_start() Twice Forces a Cookie Refresh
When you call session_start() in PHP, the framework checks whether a valid session ID already exists in the client's cookies. If it does, PHP reads that ID and resumes the associated session data without sending new headers. If no cookie is present, PHP generates a new session ID and sends a Set-Cookie header to the browser. This behavior is well-documented and predictable under normal circumstances.
However, a subtle edge case arises when you invoke session_start() more than once within the same script execution. Specifically, if your script calls session_start() followed by session_write_close(), and then does it again, the server will always send a Set-Cookie headerâeven on subsequent requests where a session cookie already exists.
The First Call: Normal Behavior
Consider this minimal script running under PHP-FPM with Apache:
<?php
session_start();
session_write_close();
die();
?>
On the first request, the browser receives a Set-Cookie header containing the PHPSESSID. On all following requests to the same URL, no cookie header appears because PHP finds an existing session ID and simply resumes it. This is exactly what you'd expect.
The Second Call: Unexpected Behavior
Now modify the script to call session_start() twice:
<?php
session_start();
session_write_close();
session_start();
session_write_close();
die();
?>
With this change, every requestâincluding those where a valid session cookie is already presentâtriggers a new Set-Cookie header. The session ID value remains the same, but the unnecessary header transmission can impact performance and complicate client-side logic that relies on the absence of cookies for certain flows.
What's Happening Under the Hood
The root cause lies in how PHP handles multiple session_start() calls. According to the PHP manual, calling session_start() multiple times is allowed only if the first call did not set a cookie (i.e., no session was found). Once a session has been started and closed via session_write_close(), subsequent calls to session_start() will re-initialize the session handler, which includes regenerating the session ID and forcing a cookie update.
From PHP 7.0 onward, calling session_start() after session_write_close() is explicitly documented as triggering a new session initialization. This means:
- A new session ID may be generated if regeneration is enabled or forced by configuration.
- The
Set-Cookieheader is always sent because PHP treats the second call as starting a fresh session, regardless of whether a cookie exists.
This behavior is not a bug; it's by design. However, it can easily lead to unintended side effects if developers assume that multiple calls will behave idempotently.
Practical Implications and Best Practices
If you need to ensure a session is started without risking unnecessary cookie updates, follow these guidelines:
- Call
session_start()exactly once per request. There is rarely a valid reason to start the session multiple times unless you're explicitly regenerating the session ID. - Avoid calling
session_write_close()followed by anothersession_start()unless regeneration is intended. If you close and reopen the session, PHP will treat it as a new session initialization. - Use
session_regenerate_id(false)only when necessary. Callingsession_regenerate_id(true)will generate a new ID and force a cookie update, which may not be what you want in all contexts.
Example of safe usage:
<?php
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
// Use the session here
session_write_close();
?>
This pattern ensures that session_start() is only called if no session exists, preventing unintended cookie headers.
When Might You Need Multiple Calls?
In rare cases, developers might want to start a session, perform some logic, close it, and then start it again with different settings (e.g., changing the save path or handler). In such scenarios, be aware that the second call will always send a cookie header. If this is undesirable, consider restructuring your code to avoid closing and reopening the session unnecessarily.
For projects using frameworks like Laravel, the StartSession middleware automatically handles session initialization, so you typically don't need to call session_start() manually. However, in vanilla PHP or custom scripts, understanding these nuances helps prevent subtle issues related to cookie management and session state.
