Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Session Functions Reject NUL Bytes And session_encode() Changes Its Empty-Session Return Value

PHP hardens several session functions against embedded NUL bytes and tightens session_encode()’s return value. Until PHP 8.6, session_module_name() silently truncated a $name argument containing a NUL byte at the first null character and emitted only a generic "module not found" warning; in PHP 8.6, it throws a ValueError instead. Separately, calling session_encode() on a session with no data in $_SESSION used to return false, the same value returned on an actual encoding failure; in PHP 8.6, it returns an empty string "" for an empty session, reserving false for genuine encoding failures. The same NUL-byte hardening theme also applies to the session.cookie_path, session.cookie_domain and session.cache_limiter INI settings, which now emit a warning when a NUL byte is embedded in their value.

PHP code

<?php

$moduleName = shell_exec(PHP_BINARY . ' -n -r ' . escapeshellarg('var_dump(session_module_name("foo" . chr(0) . "bar"));'));
echo $moduleName;

$dir = sys_get_temp_dir() . '/sessionNulByteAndEncodeChanges_' . getmypid();
mkdir($dir);
$code = 'session_save_path(' . var_export($dir, true) . '); session_start(); var_dump(session_encode()); session_destroy();';
$encoded = shell_exec(PHP_BINARY . ' -n -r ' . escapeshellarg($code));
echo $encoded;
array_map('unlink', glob($dir . '/*'));
rmdir($dir);

?>

Before


Warning: session_module_name(): Session handler module "foo" cannot be found in Command line code on line 1
bool(false)
bool(false)

After


Fatal error: Uncaught ValueError: session_module_name(): Argument #1 ($module) must not contain any null bytes in Command line code:1
Stack trace:
#0 Command line code(1): session_module_name('foo\x00bar')
#1 {main}
  thrown in Command line code on line 1
string(0) "" 

PHP version change

This behavior changed in 8.6.

See Also

Error Messages

Extension

© 2023-2026, Damien Seguy - Exakat