ratchetphp/Ratchet · error · UnexpectedValueException
invalid data, remaining:
Error message
invalid data, remaining:
What it means
PhpHandler::unserialize() decodes a 'php'-serialized session string, which requires every key to be delimited from its value by a '|' character. This \UnexpectedValueException fires when, at the current parse offset, no '|' remains in the remaining raw data — meaning the session payload is truncated, corrupted, or was not produced with the 'php' serialize_handler (e.g. php_serialized or php_binary format). The malformed segment cannot be mapped to a key/value pair, so parsing aborts with the leftover data reported in the message.
Solutions
- Verify the session data was encoded with the same handler: encode with PhpHandler::serialize() and the ini setting session.serialize_handler=php before decoding.
- Inspect the raw payload for truncation or corruption (missing '|' delimiters), e.g. log it, and discard/regenerate the session when it is invalid.
- Wrap unserialize() in a try/catch for \UnexpectedValueException and treat it as an expired/invalid session: clear the data and start a fresh session instead of failing the request.
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at src/Ratchet/Session/Serialize/PhpHandler.php:34 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of ratchetphp/Ratchet@e621c6c40b (2026-09-16).
Data as JSON: /api/errors/a5e38a7385e14b5e.
Report an issue: GitHub.
Appendix: source
Thrown at src/Ratchet/Session/Serialize/PhpHandler.php:34
}
$serialized = implode('', $preSerialized);
}
return $serialized;
}
/**
* {@inheritdoc}
* @link http://ca2.php.net/manual/en/function.session-decode.php#108037 Code from this comment on php.net
* @throws \UnexpectedValueException If there is a problem parsing the data
*/
public function unserialize($raw) {
$returnData = array();
$offset = 0;
while ($offset < strlen($raw)) {
if (!strstr(substr($raw, $offset), "|")) {
throw new \UnexpectedValueException("invalid data, remaining: " . substr($raw, $offset));
}
$pos = strpos($raw, "|", $offset);
$num = $pos - $offset;
$varname = substr($raw, $offset, $num);
$offset += $num + 1;
// try to unserialize one piece of data from current offset, ignoring any warnings for trailing data on PHP 8.3+
// @link https://wiki.php.net/rfc/unserialize_warn_on_trailing_data
$data = @unserialize(substr($raw, $offset));
$returnData[$varname] = $data;
$offset += strlen(serialize($data));
}
return $returnData;
}
}View on GitHub (pinned to e621c6c40b)