Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion ext/session/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,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;

Expand Down Expand Up @@ -462,6 +463,7 @@ static zend_result php_session_initialize(void)
}
return FAILURE;
}
is_new_id = true;
if (PS(use_cookies)) {
PS(send_cookie) = true;
}
Expand All @@ -478,6 +480,7 @@ static zend_result php_session_initialize(void)
}
return FAILURE;
}
is_new_id = true;
if (PS(use_cookies)) {
PS(send_cookie) = true;
}
Expand Down Expand Up @@ -507,7 +510,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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this handler is identical to the one in lazy_write_new_session_id_no_id.phpt. Could merge both cases into a single test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I specifically put them into separate files to have simpler to understand different paths.

{
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
Loading