Commit 67fb87b3517 for php

commit 67fb87b3517585825a78f35e6bee5a0bed7010e1
Author: Jorg Sowa <jorg.sowa@gmail.com>
Date:   Fri Dec 4 18:12:40 2026 +0100

    ext/session: Fix new empty sessions never being stored with lazy_write and strict mode (GH-25433)

    With lazy_write, an empty new session triggered updateTimestamp() instead
    of write(). Handlers whose updateTimestamp() does not create records never
    stored it, so strict mode rejected the ID and issued a new one on every
    request.

    Closes GH-25433

diff --git a/NEWS b/NEWS
index 16fba1b9ca9..11708cfe2c0 100644
--- a/NEWS
+++ b/NEWS
@@ -18,6 +18,10 @@ PHP                                                                        NEWS
   . Fixed type inference of JIT trace after FE_FETCH_R[W] with identical
     branches. (ilutov, Arnaud)

+- Session:
+  . Fixed new empty sessions never being stored with lazy_write and strict
+    mode. (Jorg Sowa)
+
 - Standard:
   . Fixed bug GH-25959 ("consumed" stream filter loses or repeats data).
     (Artem Ukrainskiy)
diff --git a/ext/session/session.c b/ext/session/session.c
index dbd0310d278..24959d7a7cd 100644
--- a/ext/session/session.c
+++ b/ext/session/session.c
@@ -436,6 +436,7 @@ static zend_long php_session_gc(bool immediate)
 static zend_result php_session_initialize(void)
 {
 	zend_string *val = NULL;
+	bool is_new_id = false;

 	PS(session_status) = php_session_active;

@@ -470,6 +471,7 @@ static zend_result php_session_initialize(void)
 			}
 			return FAILURE;
 		}
+		is_new_id = true;
 		if (PS(use_cookies)) {
 			PS(send_cookie) = true;
 		}
@@ -486,6 +488,7 @@ static zend_result php_session_initialize(void)
 			}
 			return FAILURE;
 		}
+		is_new_id = true;
 		if (PS(use_cookies)) {
 			PS(send_cookie) = true;
 		}
@@ -515,7 +518,8 @@ static zend_result php_session_initialize(void)
 		PS(session_vars) = NULL;
 	}
 	if (val) {
-		if (PS(lazy_write)) {
+		/* A new ID has no stored record yet, so lazy_write must not skip the write */
+		if (PS(lazy_write) && !is_new_id) {
 			PS(session_vars) = zend_string_copy(val);
 		}
 		php_session_decode(val);
diff --git a/ext/session/tests/user_session_module/lazy_write_new_session_id_no_id.phpt b/ext/session/tests/user_session_module/lazy_write_new_session_id_no_id.phpt
new file mode 100644
index 00000000000..6d87c88a691
--- /dev/null
+++ b/ext/session/tests/user_session_module/lazy_write_new_session_id_no_id.phpt
@@ -0,0 +1,41 @@
+--TEST--
+lazy_write stores a new empty session when no ID is given
+--EXTENSIONS--
+session
+--INI--
+session.use_strict_mode=1
+session.lazy_write=1
+session.use_cookies=0
+session.cache_limiter=
+--FILE--
+<?php
+class Handler implements SessionHandlerInterface, SessionIdInterface, SessionUpdateTimestampHandlerInterface
+{
+    private array $storage = [];
+
+    public function open($path, $name): bool { return true; }
+    public function close(): bool { return true; }
+    public function read($id): string|false { return $this->storage[$id] ?? ''; }
+    public function write($id, $data): bool { echo "write\n"; $this->storage[$id] = $data; return true; }
+    public function destroy($id): bool { return true; }
+    public function gc($max_lifetime): int|false { return 0; }
+    public function create_sid(): string { return bin2hex(random_bytes(16)); }
+    public function validateId($id): bool { return isset($this->storage[$id]); }
+    public function updateTimestamp($id, $data): bool { echo "updateTimestamp\n"; return true; }
+}
+
+session_set_save_handler(new Handler());
+
+session_start();
+$id = session_id();
+session_commit();
+
+session_id($id);
+session_start();
+var_dump(session_id() === $id);
+session_commit();
+?>
+--EXPECT--
+write
+bool(true)
+updateTimestamp
diff --git a/ext/session/tests/user_session_module/lazy_write_new_session_id_rejected.phpt b/ext/session/tests/user_session_module/lazy_write_new_session_id_rejected.phpt
new file mode 100644
index 00000000000..df0b2263710
--- /dev/null
+++ b/ext/session/tests/user_session_module/lazy_write_new_session_id_rejected.phpt
@@ -0,0 +1,44 @@
+--TEST--
+lazy_write stores a new empty session when strict mode rejects the ID
+--EXTENSIONS--
+session
+--INI--
+session.use_strict_mode=1
+session.lazy_write=1
+session.use_cookies=0
+session.cache_limiter=
+--FILE--
+<?php
+class Handler implements SessionHandlerInterface, SessionIdInterface, SessionUpdateTimestampHandlerInterface
+{
+    private array $storage = [];
+
+    public function open($path, $name): bool { return true; }
+    public function close(): bool { return true; }
+    public function read($id): string|false { return $this->storage[$id] ?? ''; }
+    public function write($id, $data): bool { echo "write\n"; $this->storage[$id] = $data; return true; }
+    public function destroy($id): bool { return true; }
+    public function gc($max_lifetime): int|false { return 0; }
+    public function create_sid(): string { return bin2hex(random_bytes(16)); }
+    public function validateId($id): bool { return isset($this->storage[$id]); }
+    public function updateTimestamp($id, $data): bool { echo "updateTimestamp\n"; return true; }
+}
+
+session_set_save_handler(new Handler());
+
+session_id('unknown');
+session_start();
+var_dump(session_id() !== 'unknown');
+$id = session_id();
+session_commit();
+
+session_id($id);
+session_start();
+var_dump(session_id() === $id);
+session_commit();
+?>
+--EXPECT--
+bool(true)
+write
+bool(true)
+updateTimestamp