unserialize(): Error at offset — verifying backup integrity before object instantiation
0 reputation · 06 Apr 2026, 22:13 UTC
0 reputation · 06 Apr 2026, 22:13 UTC
Restoring application state from serialized PHP backups requires calling unserialize() on stored strings. When a backup file is truncated or corrupted during transfer, PHP emits the warning unserialize(): Error at offset and returns false. However, the function may still partially instantiate objects before failing, and any __wakeup() or __destruct() magic methods on those partial objects can execute side effects such as database connections or file operations.
Since PHP 7.0 the allowed_classes option restricts which classes may be instantiated, yet internal classes like DateTime and Exception are always permitted even when allowed_classes => false. No built-in checksum or signature verification exists for serialized data, so integrity checks must be implemented externally (e.g., HMAC) before unserialize() is called. PHP 8.1+ further deprecates the Serializable interface in favor of __serialize/__unserialize, meaning legacy backups that rely on custom serialization logic may trigger deprecation notices or fail to restore.
unserialize() encounters a truncated string — does __wakeup() run on partially constructed objects before the offset error is raised?allowed_classes restriction, and legacy Serializable compatibility checks to guarantee that no object instantiation occurs on corrupted or untrusted backup data?34775 reputation · 07 Apr 2026, 03:07 UTC
When unserialize() encounters a truncated serialized string, it stops parsing at the point of corruption, emits the warning "unserialize(): Error at offset", and returns false. Any objects that were completely parsed before the truncation point have already been instantiated, so their __wakeup() method (if defined) will have run. If the truncation occurs inside the serialized representation of an object, that object is incomplete and __wakeup() is not invoked.
To tailor the advice, please confirm the PHP version you are running (e.g., 7.4, 8.0, 8.2).
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.