codin / session
Session wrapper
0.1.0
2021-03-29 14:24 UTC
Requires
- php: >=7.3
- psr/http-message: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: @stable
- phpspec/phpspec: @stable
- phpstan/phpstan: @stable
Suggests
- ext-redis: Redis session storage
- league/flysystem: File system session storage
This package is auto-updated.
Last update: 2024-10-29 06:06:12 UTC
README
Session data is stored as a json encoded string.
- not affected by PHP serialization RCE attacks
Quick start using array session storage
use Session\{
Session,
Cookies,
Storage\ArrayStorage
};
$session = new Session(new Cookies, new ArrayStorage);
$session->start();
$session->put('foo', 'bar');
echo $session->get('foo'); // output "bar"
$session->remove('foo');
$b = $session->get('foo', 'baz');
echo $b; // output "baz"
Closing the session and setting the cookie
$session->close();
header('Set-Cookie', $session->cookie());
# Using PSR7 Response
$session->close();
$response = new Psr\Http\Message\Response;
$response->withAddedHeader('Set-Cookie', $session->cookie());
Session storage handlers
Redis example
use Session\{
Session,
Cookies,
Storage\RedisStorage
};
$redis = new \Redis;
$ttl = 3600;
$storage = new RedisStorage(redis, $ttl);
$session = new Session(new Cookies, $storage);
File storage example
use Session\{
Session,
Cookies,
Storage\FilesystemStorage
};
$ttl = 3600;
$adapter = new \League\Flysystem\Adapter\Local('/path/to/sessions/');
$filesystem = new \League\Flysystem\Filesystem($adapter);
$storage = new FilesystemStorage($filesystem, $ttl);
// remove expired sessions
$storage->purge();
$session = new Session(new Cookies, $storage);