| 1 |
PHP session serialization formats |
| 2 |
================================= |
| 3 |
|
| 4 |
These are the currently available formats in basic PHP. |
| 5 |
|
| 6 |
In the older formats, you can see the legacy of PHP's old "register random |
| 7 |
global variables for the session" mechanism in the fact that they have the |
| 8 |
ability to indicate unset-but-present as a distinct value. |
| 9 |
|
| 10 |
|
| 11 |
php |
| 12 |
--- |
| 13 |
|
| 14 |
This is PHP's default format. Set values are encoded as |
| 15 |
`{$key}|{$serializedValue}`, while unset values are `{$key}!` and placed at the |
| 16 |
end of the string. An attempt to insert unset-but-present in the middle of the |
| 17 |
string will append it to the next key (since 5.2.6 or earlier, anyway). |
| 18 |
|
| 19 |
For example, `foo|i:1;baz|s:6:"string";bar!` encodes session data with |
| 20 |
'foo' = 1, 'baz' = "string", and 'bar' is present but unset. |
| 21 |
|
| 22 |
Due to these delimiters, keys may not contain pipe (`|`) or bang (`!`) characters. |
| 23 |
Attempting to serialize a `$_SESSION` array containing these characters will |
| 24 |
fail. |
| 25 |
|
| 26 |
Keys also may not be numeric. Any numeric entries in `$_SESSION` will be ignored. |
| 27 |
|
| 28 |
There is no value length or delimiter. |
| 29 |
|
| 30 |
|
| 31 |
php_binary |
| 32 |
---------- |
| 33 |
|
| 34 |
Keys are encoded as a byte length (up to 127) followed by the indicated number |
| 35 |
of bytes. If the variable is unset, the high bit of the length is set. |
| 36 |
Otherwise, the serialized value follows immediately after the end of the key. |
| 37 |
|
| 38 |
Depending on the version of PHP, an unset-but-present might ignore the key, |
| 39 |
might set the key to null, or might stop processing the rest of the string |
| 40 |
entirely. This library takes the first option. |
| 41 |
|
| 42 |
For example, `\x03fooi:1;\x03bazs:6:"string";\x83bar` encodes session data with |
| 43 |
'foo' = 1, 'baz' = "string", and 'bar' is present but unset. |
| 44 |
|
| 45 |
Due to the size of the length field, keys may not be more than 127 bytes long. |
| 46 |
Entries in `$_SESSION` with longer keys are ignored. |
| 47 |
|
| 48 |
Keys also may not be numeric. Any numeric entries in `$_SESSION` will be ignored. |
| 49 |
|
| 50 |
There is no value length or delimiter. |
| 51 |
|
| 52 |
|
| 53 |
php_serialize |
| 54 |
------------- |
| 55 |
|
| 56 |
This is the most reliable format, added in PHP 5.5.4. The format is just |
| 57 |
`$_SESSION` passed to the standard [`serialize()`][serialize] function. It does |
| 58 |
not have the ability to indicate unset-but-present. |
| 59 |
|
| 60 |
For example, `a:2:{s:3:"foo";i:1;s:3:"baz";s:6:"string";}` encodes session data |
| 61 |
with 'foo' = 1 and 'baz' = "string". |
| 62 |
|
| 63 |
Unlike other formats, numeric keys are allowed and are stored correctly. |
| 64 |
|
| 65 |
When decoding the session, PHP does not check that the encoded string encodes |
| 66 |
an array, and will happily set `$_SESSION` to other types. It will refuse to |
| 67 |
re-serialize such a `$_SESSION`, however. |
| 68 |
|
| 69 |
|
| 70 |
wddx |
| 71 |
---- |
| 72 |
|
| 73 |
When WDDX support is compiled into PHP, the WDDX format may be used to store |
| 74 |
session data. This format, however, cannot represent the full range of PHP data |
| 75 |
types (e.g. `INF`, `NAN`, data structures containing references) and so is not |
| 76 |
likely to be particularly useful unless you're trying to share saved session |
| 77 |
data with code in some other language that has WDDX support. |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
--- |
| 82 |
[serialize]: https://php.net/manual/en/function.serialize.php |