PHP How can I create multiple sessions?

PHP How can I create multiple sessions?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

The Illusion of Multiple Sessions in PHP

Your current approach attempts to manipulate the session lifecycle by calling session_start(), modifying data, and then explicitly closing the session with session_write_close() before starting a new one. While this logic seems sound on the surface—create, close, repeat—the behavior you are observing is a direct result of how PHP handles session identifiers and cookies.

When you call session_start() for the first block, PHP generates a unique session ID (unless SESSION_START has been disabled or the ID was manually set via headers). This ID is sent to the browser as a cookie, typically named after the value returned by session_name(). In your code, the first block sets the name to "session1". The second block changes the name to "session2".

The critical issue arises here. When you switch the session name from "session1" to "session2", PHP does not automatically create a new session file or generate a new ID for that specific request if a cookie already exists with the same domain/path but a different name. More importantly, the browser's cookie management is strict regarding names. If you set two cookies with the same name in different domains, it works. If you change the name and then try to switch back, the browser may be confused about which session ID corresponds to which name if the underlying storage mechanism isn't strictly isolated.

However, the specific output you are seeing—where the third block outputs 2 instead of 1—indicates that the session identifier associated with the browser is not changing between blocks. When you call session_start() the second time, PHP looks for an existing cookie matching the current configuration. If the previous session_write_close() didn't invalidate the cookie or if the browser retains a cookie named "session2" from the second block when you switch back to "session1", PHP might be reading from the wrong storage context or reusing the session ID incorrectly depending on how the headers were sent.

Actually, the most likely cause of your specific output is simpler: PHP does not support multiple active sessions for a single browser context in the way you are attempting.

When you execute session_name("session2") and start a new session, PHP overwrites the session cookie name. If you then switch back to session_name("session1"), you are instructing PHP to use the "session1" identifier again. However, if the browser has already received a cookie named "session2" in the previous step (which it has, because you set it), and you switch names, the session logic can become brittle.

The real problem is that session_start() relies on the existence of a session ID to resume an existing session or create a new one. If you change the session name, you are essentially changing the cookie name. When you switch back, you expect PHP to find a session with the old name. But if the browser stores cookies based on the domain and path, and you are toggling names, you might be hitting limitations in how the client sends the Cookie: PHPSESSID=... or similar header back to the server.

To achieve true isolation where each block writes to distinct files and reads from them independently without interference, you must ensure that the session ID changes between blocks. You cannot simply rely on changing the name; the ID itself must be different if you want separate storage buckets.

If your goal is to simulate multiple independent sessions within a single script execution for testing or state management purposes, consider generating a unique session ID manually before calling session_start(). This forces PHP to create a fresh session file with that specific ID, regardless of previous cookies.

// Force a new session ID and name combination
session_id(uniqid()); 
session_name("session1");
session_start();
$_SESSION["name"] = "1";
echo print_r($_SESSION, true);
session_write_close();

// Now force another unique ID for the second block
session_id(uniqid()); 
session_name("session2");
session_start();
$_SESSION["name"] = "2";
echo print_r($_SESSION, true);
session_write_close();

// Switch back to the first name, but with a NEW unique ID
session_id(uniqid()); 
session_name("session1");
session_start();
echo print_r($_SESSION, true);

By explicitly calling session_id(), you break the link to any previously stored cookie in the browser. This ensures that every block operates on a completely isolated session file. When the third block runs with a fresh ID but the "session1" name, it will correctly output 1 because it is writing to and reading from a new file, not the one polluted by the second block's activity.

This approach trades convenience for isolation. You lose the ability to persist data across requests using standard session cookies unless you manage the IDs yourself. In production environments, you generally should not switch session names or IDs arbitrarily; instead, use separate variables or a structured data model within a single session if you need to maintain multiple states. If you require distinct sessions for different users or contexts, rely on unique user identifiers passed via headers or URL parameters to look up the correct session file, rather than toggling PHP's built-in session mechanism.

For complex state management in PHP applications, frameworks like Laravel provide robust solutions for handling concurrency and state without manually wrestling with session_write_close(). Their architecture abstracts these low-level details, allowing you to manage multiple "contexts" or users safely within a single request lifecycle.

Our network