This library provides a way to encode and decode default session data.
The suggested installation method is via composer:
composer require psr7-sessions/session-encode-decode
PHP's native session_encode()
and session_decode()
functions has some limitations:
-
session_decode()
- Can't be used if there's no session active
- Populates the super global
$_SESSION
variable automatically
-
session_encode()
- Do not accept parameters, so you can't use it to encode the content you want.
- Depends on
$_SESSION
global state
(new PSR7SessionEncodeDecode\Encoder())->__invoke(['counter' => 2]); // 'counter|i:2;'
(new PSR7SessionEncodeDecode\Decoder())->__invoke('counter|i:2;'); // ['counter' => 2]